G
G
galithr2016-11-04 10:16:24
Yii
galithr, 2016-11-04 10:16:24

Is this component structure correct?

There is an API, each request to it (in addition to a link to the API itself) consists of:
- the name of the controller to which you need to contact
- the name of the method to be called
- parameters (some methods have mandatory parameters, some do not)
Actually, you need to implement the component allowing to form these requests, poison them and receive a response. I decided to split this component into three classes:
Request class - allows the user to "construct" the request body

сlass Query implements QueryInterface {
  // объект клиента
  protected $client;
  
  // конструктор сохраняющий указатель на клиент
  public function __construct(ClientInterface $client);
  
  // возвращает название контроллера к которому необходимо обратиться
  public function getController();   
  
  // возвращает название метода который нужно вызвать
  public function getMethod();    
  
  // возвращает массив параметров запроса
  public function getParams();    
  
  // выполняет запрос
  public function execute() {
     $this->client->execute($this);
  }        
  
  // устанавливает значение параметра
  public function setId($value);
}

Base client class - responsible for generating and sending an http request
сlass BaseClient implements ClientInterface {

  // формирует базовый http-запрос
  public function prepareRequest();
  
  // добавляет в базовый http-запрос параметры с класса Query, 
  //отправляет запрос на сервер и возвращает результат
  public function send(QueryInterface $query);
}

The client class itself containing the tools to call all requests
class Client extends BaseClient {

  // создает объект запроса
  public function findById($id){
    return new Query($this);
  }
}

It can be used like this:
$client = new Client();
$client->findById(1)->execute();

But the following moments strain:
1. Interdependence between classes Client and Query turns out. Or is there nothing wrong with that?
2. There will be a lot of requests - a couple of hundred, respectively, the Client class will contain many methods. Wouldn't the class be very bloated because of this?
3. In general, is such a component structure a normal practice?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question