/**
  * Executes query
  * 
  * @param AllegroApi $api
  * @return int
  */
 public function execute(AllegroApi $api)
 {
     $result = $api->searchItems($this->query['query'], isset($this->query['filters']) ? $this->query['filters'] : null);
     $response = ResponseStorage::getInstance();
     $allegroIds = array_map(function ($item) {
         return $item['allegroId'];
     }, $this->responses);
     $filteredData = [];
     foreach ($result as $allegroId => $content) {
         if (!in_array($allegroId, $allegroIds)) {
             $filteredData[$allegroId] = $content;
             $rid = $response->set(['allegroId' => $allegroId]);
             $response->hasOne($rid, QueryStorage::getInstance(), $this->queryId);
         }
     }
     if (empty($filteredData)) {
         return 0;
     }
     $response->save();
     $mail = AllegroHelper::createEmail($filteredData);
     foreach ($this->emails as $emailData) {
         mail($emailData['email'], QueryStorage::formatQuery($this->query), str_replace('[[MESSAGE]]', isset($emailData['message']) ? $emailData['message'] : '', $mail), "Content-Type: text/html; charset=ISO-8859-1\r\n");
     }
     return count($filteredData);
 }
Exemple #2
0
 private function registerCategories()
 {
     $command = $this->register('categories');
     // set description
     $command->setDescription('Shows categories');
     // set action
     $command->setCode(function (InputInterface $input, OutputInterface $output) {
         $setting = SettingsStorage::getInstance();
         $code = $setting->getById('code');
         $key = $setting->getById('key');
         if (empty($code)) {
             $code = 1;
         } else {
             $code = (int) $code['value'];
         }
         if (empty($key)) {
             $output->writeln("Allegro web api key not set, use 'config key XXXXXX' to configure");
             return;
         } else {
             $key = $key['value'];
         }
         $api = new AllegroApi($key, $code);
         $categories = $api->getCategories();
         $tree = [];
         $names = [];
         foreach ($api->getCategories() as $category) {
             if (empty($tree[$category->{"cat-parent"}])) {
                 $tree[$category->{"cat-parent"}] = [];
             }
             $tree[$category->{"cat-parent"}][] = $category->{"cat-id"};
             $names[$category->{"cat-id"}] = $category->{"cat-name"};
         }
         $this->printCategoryTree($tree, $names);
     });
 }