1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koality\ShopwarePlugin\Collector; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\DBALException; |
7
|
|
|
use Koality\ShopwarePlugin\Formatter\Result; |
8
|
|
|
|
9
|
|
|
class NewsletterSubscriptionCollector implements Collector |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $pluginConfig; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Connection |
18
|
|
|
*/ |
19
|
|
|
private $connection; |
20
|
|
|
|
21
|
|
|
public function __construct(array $pluginConfig, Connection $connection) |
22
|
|
|
{ |
23
|
|
|
$this->connection = $connection; |
24
|
|
|
$this->pluginConfig = $pluginConfig; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* |
30
|
|
|
* @throws DBALException |
31
|
|
|
*/ |
32
|
|
|
public function getResult(): Result |
33
|
|
|
{ |
34
|
|
|
$newsletterSubscriptions = $this->getNewsletterRegistrations(); |
35
|
|
|
|
36
|
|
|
if (array_key_exists('newsletterSubscriptions', $this->pluginConfig)) { |
37
|
|
|
$minNewsletterSubscriptions = $this->pluginConfig['newsletterSubscriptions']; |
38
|
|
|
} else { |
39
|
|
|
$minNewsletterSubscriptions = 0; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($newsletterSubscriptions < $minNewsletterSubscriptions) { |
43
|
|
|
$newsletterResult = new Result(Result::STATUS_FAIL, Result::KEY_NEWSLETTER_TOO_FEW, 'There were too few newsletter subscriptions yesterday.'); |
44
|
|
|
} else { |
45
|
|
|
$newsletterResult = new Result(Result::STATUS_PASS, Result::KEY_NEWSLETTER_TOO_FEW, 'There were enough newsletter subscriptions yesterday.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$newsletterResult->setLimit($minNewsletterSubscriptions); |
49
|
|
|
$newsletterResult->setObservedValue($newsletterSubscriptions); |
50
|
|
|
$newsletterResult->setObservedValueUnit('newsletters'); |
51
|
|
|
$newsletterResult->setLimitType(Result::LIMIT_TYPE_MIN); |
52
|
|
|
$newsletterResult->setType(Result::TYPE_TIME_SERIES_NUMERIC); |
53
|
|
|
|
54
|
|
|
return $newsletterResult; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return int | boolean |
59
|
|
|
* |
60
|
|
|
* @throws DBALException |
61
|
|
|
*/ |
62
|
|
|
private function getNewsletterRegistrations(): int |
63
|
|
|
{ |
64
|
|
|
$sql = 'SELECT count(*) FROM newsletter_recipient WHERE created_at >= ? AND created_at < ?'; |
65
|
|
|
|
66
|
|
|
$statement = $this->connection->executeQuery($sql, [ |
67
|
|
|
date('Y.m.d', strtotime('-1 days')), |
68
|
|
|
date('Y.m.d') |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$count = $statement->fetchColumn(); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($count === false) { |
74
|
|
|
return -1; |
75
|
|
|
} else { |
76
|
|
|
return (int)$count; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.