Created
October 5, 2016 15:59
-
-
Save ermst4r/2a874efda7e24963e247be2eebbc005c to your computer and use it in GitHub Desktop.
cronjob laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Sendt\Console\Commands; | |
| require_once __DIR__ . '/../../../application/config.php'; | |
| use Illuminate\Console\Command; | |
| use models\general\Advertiser; | |
| use models\general\AdvertiserExport; | |
| use models\general\AdvertiserExportQuery; | |
| use models\general\AdvertiserQuery; | |
| use models\general\LastExportTime; | |
| use models\general\LastExportTimeQuery; | |
| use Symfony\Component\Console\Input\InputOption; | |
| use Symfony\Component\Console\Input\InputArgument; | |
| class Leads extends Command { | |
| /** | |
| * The console command name. | |
| * | |
| * @var string | |
| */ | |
| protected $name = 'leads'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'Returns the valid leads for given export and period.'; | |
| /** | |
| * Create a new command instance. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() | |
| { | |
| parent::__construct(); | |
| } | |
| /** | |
| * Execute the console command. | |
| * | |
| * @return mixed | |
| */ | |
| public function fire() | |
| { | |
| $export_id = $this->argument('export_id'); | |
| $from_date = $this->option('from-date'); | |
| $till_date = $this->option('till-date'); | |
| // Datetime object maken van eventueel datum inputs, fout geven als het niet lukt | |
| if (isset($from_date)) { | |
| try { | |
| $from_date = new \DateTime($from_date); | |
| } catch(\Exception $e) { | |
| $this->error(sprintf("Kon '%s' niet parsen als DateTime object", $from_date)); | |
| return; | |
| } | |
| } | |
| if (isset($till_date)) { | |
| try { | |
| $till_date = new \DateTime($till_date); | |
| } catch(\Exception $e) { | |
| $this->error(sprintf("Kon '%s' niet parsen als DateTime object", $till_date)); | |
| return; | |
| } | |
| } | |
| // Advertiser ophalen | |
| $advertiser_export = AdvertiserExportQuery::create()->findPk($export_id); | |
| // Fout geven als advertiser niet gevonden kan worden | |
| if (!$advertiser_export instanceof AdvertiserExport) { | |
| $this->error(sprintf("Export met ID %s niet gevonden.", $export_id)); | |
| return; | |
| } | |
| $advertiser = $advertiser_export->getAdvertiser(); | |
| // Info outputten | |
| $this->info('Leads ophalen voor ' . $advertiser_export->getName() . " (id: " . $advertiser_export->getId() . "):"); | |
| // from_date op last_export_date zetten als we geen from_date meegeven | |
| if (!isset($from_date)) { | |
| $last_export_date = LastExportTimeQuery::create()->filterByAdvertiserExport($advertiser_export)->findOne(); | |
| if ($last_export_date instanceof LastExportTime) { | |
| $from_date = $last_export_date->getTillDate(); | |
| } | |
| } | |
| // Laat from_date zien | |
| if (isset($from_date)) { | |
| $this->comment(sprintf("Start datum: %s", $from_date->format('d-m-Y H:i'))); | |
| } else { | |
| $this->comment("Start datum: undefined"); | |
| } | |
| // Laat till_date zien | |
| if (isset($till_date)) { | |
| $this->comment(sprintf("Eind datum: %s", $till_date->format('d-m-Y H:i'))); | |
| } else { | |
| $this->comment("Eind datum: undefined"); | |
| } | |
| // Ophalen van leads | |
| $signups = $advertiser->getLeads($from_date, $till_date, $export_id); | |
| $line = sprintf("Aantal valid signups: %s", count($signups)); | |
| $this->comment($line); | |
| foreach($signups as $signup) { | |
| $person = $signup->getPerson(); | |
| $line = sprintf("id: %s, email: %s", $person->getId(), strtolower($person->getEmail())); | |
| $this->line($line); | |
| } | |
| } | |
| /** | |
| * Get the console command arguments. | |
| * | |
| * @return array | |
| */ | |
| protected function getArguments() | |
| { | |
| return [ | |
| ['export_id', InputArgument::REQUIRED, 'Export ID to get leads for.'], | |
| ]; | |
| } | |
| /** | |
| * Get the console command options. | |
| * | |
| * @return array | |
| */ | |
| protected function getOptions() | |
| { | |
| return [ | |
| ['from-date', null, InputOption::VALUE_OPTIONAL, 'Start date as \'dd-mm-yyyy\'', null], | |
| ['till-date', null, InputOption::VALUE_OPTIONAL, 'End date as \'dd-mm-yyyy\'', null], | |
| ]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment