/** * 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); }
private function registerRun() { $command = $this->register('run'); // set description $command->setDescription('Runs query'); // set arguments $command->setDefinition(array(new InputArgument('id', InputArgument::OPTIONAL, 'Query identifier'))); // set action $command->setCode(function (InputInterface $input, OutputInterface $output) { // get argument id $id = $input->getArgument('id'); $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']; } if (empty($id)) { foreach (QueryStorage::getInstance()->getAll() as $id => $data) { $newItems = (new QueryExecutor($id))->execute(new AllegroApi($key, $code)); $output->writeln("{$id}: {$newItems} new item(s) found"); } } else { // check if query exists if (!in_array($id, QueryStorage::getInstance()->getIds())) { $output->writeln("Query with id {$id} not found"); return; } $newItems = (new QueryExecutor($id))->execute(new AllegroApi($key, $code)); $output->writeln("{$newItems} new item(s) found"); } }); }