getNewsletterRegistrations()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 9
c 2
b 1
f 0
nc 2
nop 0
dl 0
loc 15
rs 9.9666
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();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...tatement::fetchColumn() has been deprecated: Use fetchOne() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
        $count = /** @scrutinizer ignore-deprecated */ $statement->fetchColumn();

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.

Loading history...
72
73
        if ($count === false) {
74
            return -1;
75
        } else {
76
            return (int)$count;
77
        }
78
    }
79
}
80