CollectorContainer::init()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 3
b 0
f 0
nc 4
nop 2
dl 0
loc 29
rs 9.7333
1
<?php
2
3
namespace Koality\ShopwarePlugin\Collector;
4
5
use Doctrine\DBAL\Connection;
6
use Koality\ShopwarePlugin\Formatter\KoalityFormatter;
7
use RuntimeException;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\Store\Services\StoreClient;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Class CollectorContainer
14
 *
15
 * @package Koality\ShopwarePlugin\Collector
16
 *
17
 * @author Nils Langner <[email protected]>
18
 * created 2020-12-29
19
 */
20
class CollectorContainer
21
{
22
    /**
23
     * @var StoreClient
24
     */
25
    private $storeClient;
26
27
    /**
28
     * @var Collector[]
29
     */
30
    private $collectors = [];
31
32
    /**
33
     * @var ContainerInterface
34
     */
35
    private $container;
36
37
    /**
38
     * CollectorContainer constructor.
39
     *
40
     * @param StoreClient $storeClient
41
     * @param ContainerInterface $container
42
     */
43
    public function __construct(StoreClient $storeClient, ContainerInterface $container)
44
    {
45
        $this->storeClient = $storeClient;
46
        $this->container = $container;
47
    }
48
49
    public function init($pluginConfig, Context $context)
50
    {
51
        $connection = $this->container->get(Connection::class);
52
53
        if (is_null($connection)) {
54
            throw new RuntimeException('Cannot establish database connection.');
55
        }
56
57
        $pluginRepository = $this->container->get('plugin.repository');
58
59
        if (is_null($pluginRepository)) {
60
            throw new RuntimeException('Cannot find plugin repository.');
61
        }
62
63
        $orderRepository = $this->container->get('order.repository');
64
65
        if (is_null($orderRepository)) {
66
            throw new RuntimeException('Cannot find order repository.');
67
        }
68
69
        $request = $this->container->get('request_stack')->getCurrentRequest();
70
71
        // the order of the components also reflects the order the metrics are shown in koality.io
72
        $this->collectors = [
73
            new CountOrdersCollector($pluginConfig, $context, $orderRepository),
74
            new ActiveProductsCollector($pluginConfig, $connection),
75
            new UpdatablePluginsCollector($pluginConfig, $pluginRepository, $context, $this->storeClient, $request),
76
            new OpenCartsCollector($pluginConfig, $connection),
77
            new NewsletterSubscriptionCollector($pluginConfig, $connection),
78
        ];
79
    }
80
81
    public function run()
82
    {
83
        $formatter = new KoalityFormatter();
84
85
        foreach ($this->collectors as $collector) {
86
            $formatter->addResult($collector->getResult());
87
        }
88
89
        return $formatter;
90
    }
91
}
92