OpenCartsCollector   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 30
c 2
b 1
f 0
dl 0
loc 78
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOpenCartCount() 0 15 2
A getResult() 0 24 3
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
/**
10
 * Class OpenCartsCollector
11
 *
12
 * @package Koality\ShopwarePlugin\Collector
13
 *
14
 * @author Nils Langner <[email protected]>
15
 * created 2020-12-28
16
 */
17
class OpenCartsCollector implements Collector
18
{
19
    /**
20
     * @var array
21
     */
22
    private $pluginConfig = [];
23
24
    /**
25
     * @var Connection
26
     */
27
    private $connection;
28
29
    /**
30
     * OpenCartsCollector constructor.
31
     *
32
     * @param array $pluginConfig
33
     * @param Connection $connection
34
     */
35
    public function __construct(array $pluginConfig, Connection $connection)
36
    {
37
        $this->pluginConfig = $pluginConfig;
38
        $this->connection = $connection;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     * @throws DBALException
44
     */
45
    public function getResult(): Result
46
    {
47
        $cartCount = $this->getOpenCartCount();
48
49
        if (array_key_exists('openCarts', $this->pluginConfig)) {
50
            $maxCartCount = $this->pluginConfig['openCarts'];
51
        } else {
52
            $maxCartCount = 1000;
53
        }
54
55
        if ($cartCount > $maxCartCount) {
56
            $cartResult = new Result(Result::STATUS_FAIL, Result::KEY_CARTS_OPEN_TOO_MANY, 'There are too many open carts at the moment.');
57
        } else {
58
            $cartResult = new Result(Result::STATUS_PASS, Result::KEY_CARTS_OPEN_TOO_MANY, 'There are not too many open carts at the moment.');
59
        }
60
61
        $cartResult->setLimit($maxCartCount);
62
        $cartResult->setObservedValue($cartCount);
63
        $cartResult->setObservedValueUnit('carts');
64
        $cartResult->setObservedValuePrecision(0);
65
        $cartResult->setLimitType(Result::LIMIT_TYPE_MAX);
66
        $cartResult->setType(Result::TYPE_TIME_SERIES_NUMERIC);
67
68
        return $cartResult;
69
    }
70
71
    /**
72
     * Return the number of open carts.
73
     *
74
     * @return int
75
     *
76
     * @throws DBALException
77
     *
78
     * @todo duplicated code with NewsletterSubscriptionCollector
79
     */
80
    private function getOpenCartCount(): int
81
    {
82
        $sql = 'SELECT count(*) FROM cart WHERE created_at >= ? AND created_at < ?';
83
84
        $statement = $this->connection->executeQuery($sql, [
85
            date('Y.m.d', strtotime('-1 hour')),
86
            date('Y.m.d')
87
        ]);
88
89
        $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

89
        $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...
90
91
        if ($count === false) {
92
            return -1;
93
        } else {
94
            return (int)$count;
95
        }
96
    }
97
}
98