| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 | 
							- <?php
 
- namespace cx_newsletter;
 
- use \wp_rest_request;
 
- class messages_get_endpoint 
 
- extends rest_endpoint
 
- implements rest_endpoint_interface {
 
-     use rest_endpoint_with_apikey;
 
-     
 
-     public function get_method() : string {
 
-         return 'GET';
 
-     }
 
-     public function get_route() : string {
 
-         return '/messages/';
 
-     }
 
-     public function action(wp_rest_request $request) : array {
 
-         if (!$this->check_request_apikey($request)) {
 
-             return $this->bad_apikey_response();
 
-         }
 
-         $response = new response_builder();
 
-         
 
-         return $response
 
-         ->set_code(response_code::SUCCESS)
 
-         ->set_status(response_status::SUCCESS)
 
-         ->set_message('List of messages to send.')
 
-         ->set_content($this->load_messages())
 
-         ->build();
 
-     }
 
-     private function load_messages() : array {
 
-         $this->prepare(); 
 
-         $next_campaign = $this->select_campaign();
 
-         if ($next_campaign === null) {
 
-             return [];
 
-         }
 
-         
 
-         $customers = $this->load_customers($next_campaign);
 
-         $list = [];
 
-         foreach ($customers as $customer) {
 
-             $count = [
 
-                 'Message' => $next_campaign->message->content,
 
-                 'PhoneNumber' => $customer->phone_number,
 
-                 'Campaign' => $next_campaign->get_id()
 
-             ];
 
-             array_push($list, $count);
 
-             // FIX FOR CxSMS app
 
-             $this->send_log_repository->mark_send($next_campaign, $customer);
 
-         }
 
-         if (empty($list)) {
 
-             $next_campaign->finalize();
 
-             $this->campaigns_repository->save($next_campaign);
 
-         }
 
-         return $list;
 
-     }
 
-     private function select_campaign() : ?campaign {
 
-         return $this->campaigns_repository->load_next(campaign_type::sms);
 
-     }
 
-     private function load_customers(campaign $next) : array {
 
-         return $this->send_log_repository->load_all(
 
-             $next,
 
-             $this->get_settings()->get('sms_count'),
 
-             false
 
-         );
 
-     }
 
-     private function prepare() : void {
 
-         $this->campaigns_repository = new campaigns_repository(
 
-             $this->get_database(),
 
-             $this->get_tables()
 
-         );
 
-         $this->send_log_repository = new send_log_repository(
 
-             $this->get_database(),
 
-             $this->get_tables()
 
-         );
 
-         
 
-         $this->customers_mapper = new customers_mapper(
 
-             $this->get_database(),
 
-             $this->get_tables()
 
-         );
 
-         $this->messages_mapper = new messages_mapper(
 
-             $this->get_database(),
 
-             $this->get_tables()
 
-         );
 
-     }   
 
-     private campaigns_repository $campaigns_repository;
 
-     private send_log_repository $send_log_repository;
 
-     private customers_mapper $customers_mapper;
 
-     private messages_mapper $messages_mapper;
 
- }
 
 
  |