AllPackages::getReport()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.9666
1
<?php
2
3
namespace BiffBangPow\SSMonitor\Server\Client;
4
5
use BiffBangPow\SSMonitor\Server\Helper\ReportingHelper;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\Core\Extensible;
8
use SilverStripe\Core\Injector\Injector;
9
use Psr\Log\LoggerInterface;
10
use SilverStripe\View\ArrayData;
11
use SilverStripe\ORM\ArrayList;
12
use SilverStripe\View\SSViewer;
13
14
class AllPackages implements MonitoringClientInterface
15
{
16
17
    use ClientCommon;
18
    use Configurable;
19
    use Extensible;
20
21
    /**
22
     * @var string
23
     */
24
    private string $clientName = 'allpackages';
0 ignored issues
show
introduced by
The private property $clientName is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @config
28
     * @var string
29
     */
30
    private static $client_title = 'All installed vendor packages';
0 ignored issues
show
introduced by
The private property $client_title is not used, and could be removed.
Loading history...
31
32
    /**
33
     * @config
34
     * @var array
35
     */
36
    private static $data_config = [];
0 ignored issues
show
introduced by
The private property $data_config is not used, and could be removed.
Loading history...
37
38
    /**
39
     * @var array
40
     */
41
    private $warnings = [];
42
43
    /**
44
     * @var array
45
     */
46
    private $clientData = [];
47
48
    public function getWarnings($data)
49
    {
50
        //Injector::inst()->get(LoggerInterface::class)->info(print_r($data, true));
51
        $this->clientData = $data;
52
        $this->checkMinVersions();
53
        $this->checkDevPackages();
54
        $allWarnings = $this->warnings;
55
        $this->extend('updateWarnings', $allWarnings, $data);
56
        return (count($allWarnings) > 0) ? $allWarnings : false;
57
    }
58
59
    public function getReport($data)
60
    {
61
        $versions = ArrayList::create();
62
63
        foreach ($data as $packageName => $version) {
64
            $versions->push([
65
                'PackageName' => $packageName,
66
                'PackageVersion' => $version
67
            ]);
68
        }
69
70
        $viewer = new SSViewer('BiffBangPow/SSMonitor/Server/Module/AllPackages');
71
        return $viewer->process(ArrayData::create([
72
            'Title' => $this->getClientTitle(),
73
            'Packages' => $versions
74
        ]));
75
    }
76
77
    /**
78
     * Check to see if any of the packages contain dev versions - this is generally a bad idea!
79
     * @return void
80
     */
81
    private function checkDevPackages()
82
    {
83
        $config = $this->config()->get('data_config');
84
        if ((isset($config['warnings']['dev_packages'])) && ($config['warnings']['dev_packages'])) {
85
            foreach ($this->clientData as $package => $version) {
86
                if (stristr($version, 'dev') !== false) {
87
                    $this->warnings[] = _t(
88
                        __CLASS__ . '.devpackages',
89
                        'Development packages may be present in the package list'
90
                    );
91
                    return;
92
                }
93
            }
94
        }
95
    }
96
97
    private function checkMinVersions()
98
    {
99
        $config = $this->config()->get('data_config');
100
        if (isset($config['warnings']['min_versions'])) {
101
            foreach ($this->clientData as $package => $version) {
102
                if (isset($config['warnings']['min_versions'][$package])) {
103
                    $threshold = $config['warnings']['min_versions'][$package];
104
                    if (ReportingHelper::isVersionLess($version, $threshold)) {
105
                        $this->warnings[] = _t(
106
                            __CLASS__ . '.corepackagewarning',
107
                            '{package} is below the required version. Required: {required}.  Actual: {actual}',
108
                            [
109
                                'package' => $package,
110
                                'required' => $threshold,
111
                                'actual' => $version
112
                            ]
113
                        );
114
                    }
115
                }
116
            }
117
        }
118
    }
119
}
120