-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateStatsCommand.php
94 lines (83 loc) · 3.47 KB
/
UpdateStatsCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace San\EmailBundle\Command;
use Goutte\Client;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateStatsCommand extends ContainerAwareCommand
{
protected function configure()
{
parent::configure();
$this->setName('san:email_send_stats')
->setDescription('Update emails stats');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = new Client();
$crawler = $client->request('GET', 'http://sendgrid.com/newsletter');
$form = $crawler->selectButton('Login')->form();
$crawler = $client->submit($form, array(
'login[username]' => $this->getContainer()->getParameter('exercise.sendgrid.api_user'),
'login[password]' => $this->getContainer()->getParameter('exercise.sendgrid.api_key'),
'referal' => '',
));
$emails = $this->getEmailSendRepository()->findAll();
$progress = $this->getHelperSet()->get('progress');
$progress->start($output, count($emails));
foreach ($emails as $emailSend) {
$response = $this->getContainer()->get('exercise.sendgrid')->get('marketing')->getEmail(array(
'name' => $emailSend->getSendgridEmailName()
))->json();
$statsFileLink = sprintf("https://sendgrid.com/newsletter/downloadExcelNewsletterStats/id/%u", $response['newsletter_id']);
$client->request('GET', $statsFileLink);
$tempFilePath = sprintf("%s/%s.xls", sys_get_temp_dir(), $response['newsletter_id']);
$tempFile = fopen($tempFilePath, 'wb');
fwrite($tempFile, $client->getResponse()->getContent());
fclose($tempFile);
$phpExcel = \PHPExcel_IOFactory::load($tempFilePath);
foreach (array(
"B2" => 'attempted',
"C2" => 'delivered',
"D2" => 'opens',
"E2" => 'uniqueOpens',
"F2" => 'clicks',
"G2" => 'uniqueClicks',
"H2" => 'ctr',
"I2" => 'tctr',
"J2" => 'bounces',
"K2" => 'spamReport',
"L2" => 'repeatSpamReport',
"M2" => 'unsubscribes',
"N2" => 'repeatBounces',
"O2" => 'invalidEmail',
) as $position => $method) {
$value = str_replace('%', '', $phpExcel->getActiveSheet()->getCell($position)->getValue());
$emailSend->{sprintf("set%s", $method)}($value);
}
unlink($tempFilePath);
$progress->advance();
}
$this->getOm()->flush();
$progress->finish();
}
/**
* @return \San\EmailBundle\Repository\EmailSendRepository
*/
protected function getEmailSendRepository()
{
return $this->getOm()->getRepository("SanEmailBundle:EmailSend");
}
/**
* @return \Doctrine\Common\Persistence\ObjectManager
*/
protected function getOm()
{
if ($this->getContainer()->get('san.admin.email_send')->getManager() == 'orm') {
return $this->getContainer()->get('doctrine.orm.entity_manager');
}
return $this->getContainer()->get('doctrine.odm.mongodb.document_manager');
}
}