Completed
Push — master ( da2253...983ced )
by Daniel
01:54
created

ComposerPackagesListing   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 31
c 7
b 1
f 3
lcom 1
cbo 0
dl 0
loc 192
rs 9.8

13 Methods

Rating   Name   Duplication   Size   Complexity  
A decisionPackageOrPackageDev() 0 7 2
A exposeEnvironmentDetails() 0 14 1
A exposePhpDetails() 0 23 3
A getFileModifiedTimestampOfFile() 0 14 4
A getPackageDetailsFromGivenComposerLockFile() 0 20 4
A getPkgAging() 0 6 1
A getPkgBasicInfo() 0 9 3
A getPkgFileInListOfPackageArrayOut() 0 6 1
A getPkgLcns() 0 7 2
A getPkgOptAtributeAll() 0 11 3
A getPkgTiming() 0 10 2
A getPkgVerNo() 0 10 3
A getPkgVersion() 0 9 2
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\composer_packages_listing;
30
31
/**
32
 * usefull functions to get quick results
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait ComposerPackagesListing
37
{
38
39
    /**
40
     * Decision between Main or Development packages
41
     *
42
     * @param boolean $devInstead
43
     * @return string
44
     */
45
    private function decisionPackageOrPackageDev($devInstead) {
46
        $sReturn = 'packages';
47
        if ($devInstead) {
48
            $sReturn = 'packages-dev';
49
        }
50
        return $sReturn;
51
    }
52
53
    /**
54
     * Exposes few Environment details
55
     *
56
     * @return array
57
     */
58
    protected function exposeEnvironmentDetails() {
59
        $knownValues = [
60
            'AMD64' => 'x64 (64 bit)',
61
            'i386'  => 'x86 (32 bit)',
62
            'i586'  => 'x86 (32 bit)',
63
        ];
64
        return [
65
            'Host Name'                     => php_uname('n'),
66
            'Machine Type'                  => php_uname('m'),
67
            'Operating System Architecture' => $knownValues[php_uname('m')],
68
            'Operating System Name'         => php_uname('s'),
69
            'Operating System Version'      => php_uname('r') . ' ' . php_uname('v'),
70
        ];
71
    }
72
73
    /**
74
     *
75
     * @return array
76
     */
77
    protected function exposePhpDetails($skipAging = false) {
78
        $aReturn = [
79
            'Aging'           => $this->getPkgAging($this->getFileModifiedTimestampOfFile(PHP_BINARY, 'Y-m-d')),
80
            'Architecture'    => (PHP_INT_SIZE === 4 ? 'x86 (32 bit)' : 'x64 (64 bit)'),
81
            'Description'     => 'PHP is a popular general-purpose scripting language'
82
            . ' that is especially suited to web development',
83
            'Homepage'        => 'https://secure.php.net/',
84
            'License'         => 'PHP License v3.01',
85
            'Package Name'    => 'ZendEngine/PHP',
86
            'Product'         => 'PHP',
87
            'Time'            => $this->getFileModifiedTimestampOfFile(PHP_BINARY, 'l, d F Y H:i:s'),
88
            'Time as PHP no.' => $this->getFileModifiedTimestampOfFile(PHP_BINARY, 'PHPtime'),
89
            'Type'            => 'scripting language',
90
            'Url'             => 'https://github.com/php/php-src',
91
            'Vendor'          => 'The PHP Group',
92
            'Version'         => PHP_VERSION,
93
            'Version no.'     => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
94
        ];
95
        if ($skipAging) {
96
            unset($aReturn['Aging']);
97
        }
98
        return $aReturn;
99
    }
100
101
    /**
102
     * Returns Modified date and time of a given file
103
     *
104
     * @param string $fileName
105
     * @param string $format
106
     * @param boolean $resultInUtc
107
     * @return string
108
     */
109
    protected function getFileModifiedTimestampOfFile($fileName, $format = 'Y-m-d H:i:s', $resultInUtc = false) {
110
        if (!file_exists($fileName)) {
111
            return ['error' => $fileName . ' was not found'];
112
        }
113
        $info = new \SplFileInfo($fileName);
114
        if ($format === 'PHPtime') {
115
            return $info->getMTime();
116
        }
117
        $sReturn = date($format, $info->getMTime());
118
        if ($resultInUtc) {
119
            $sReturn = gmdate($format, $info->getMTime());
120
        }
121
        return $sReturn;
122
    }
123
124
    /**
125
     * Returns a complete list of packages and respective details from a composer.lock file
126
     *
127
     * @param string $fileToRead
128
     * @return array
129
     */
130
    protected function getPackageDetailsFromGivenComposerLockFile($fileToRead, $devInstead = false, $skipAging = false) {
131
        if (!file_exists($fileToRead)) {
132
            return ['error' => $fileToRead . ' was not found'];
133
        }
134
        $dNA      = '---';
135
        $alnfo    = [];
136
        $packages = $this->getPkgFileInListOfPackageArrayOut($fileToRead);
137
        foreach ($packages[$this->decisionPackageOrPackageDev($devInstead)] as $value) {
138
            $atr                   = $this->getPkgOptAtributeAll($value, $dNA);
139
            $basic                 = $this->getPkgBasicInfo($value, $dNA);
140
            $vrs                   = $this->getPkgVersion($value, $dNA);
141
            $alnfo[$value['name']] = array_merge($atr, $basic, $vrs, $this->getPkgTiming($value, $dNA));
142
            if ($skipAging) {
143
                unset($alnfo[$value['name']]['Aging']);
144
            }
145
            ksort($alnfo[$value['name']]);
146
        }
147
        ksort($alnfo);
148
        return $alnfo;
149
    }
150
151
    private function getPkgAging($timePkg) {
152
        $dateTimeToday = new \DateTime(date('Y-m-d', strtotime('today')));
153
        $dateTime      = new \DateTime(date('Y-m-d', strtotime($timePkg)));
154
        $interval      = $dateTimeToday->diff($dateTime);
155
        return $interval->format('%a days ago');
156
    }
157
158
    private function getPkgBasicInfo($value, $defaultNA) {
159
        return [
160
            'License'      => (isset($value['license']) ? $this->getPkgLcns($value['license']) : $defaultNA),
161
            'Package Name' => $value['name'],
162
            'PHP required' => (isset($value['require']['php']) ? $value['require']['php'] : $defaultNA),
163
            'Product'      => explode('/', $value['name'])[1],
164
            'Vendor'       => explode('/', $value['name'])[0],
165
        ];
166
    }
167
168
    private function getPkgFileInListOfPackageArrayOut($fileToRead) {
169
        $handle       = fopen($fileToRead, 'r');
170
        $fileContents = fread($handle, filesize($fileToRead));
171
        fclose($handle);
172
        return json_decode($fileContents, true);
173
    }
174
175
    private function getPkgLcns($license) {
176
        $lcns = $license;
177
        if (is_array($license)) {
178
            $lcns = implode(', ', $license);
179
        }
180
        return $lcns;
181
    }
182
183
    private function getPkgOptAtributeAll($value, $defaultNA) {
184
        $attr    = ['description', 'homepage', 'type', 'url', 'version'];
185
        $aReturn = [];
186
        foreach ($attr as $valueA) {
187
            $aReturn[ucwords($valueA)] = $defaultNA;
188
            if (array_key_exists($valueA, $value)) {
189
                $aReturn[ucwords($valueA)] = $value[$valueA];
190
            }
191
        }
192
        return $aReturn;
193
    }
194
195
    private function getPkgTiming($value, $defaultNA) {
196
        if (isset($value['time'])) {
197
            return [
198
                'Aging'           => $this->getPkgAging($value['time']),
199
                'Time'            => date('l, d F Y H:i:s', strtotime($value['time'])),
200
                'Time as PHP no.' => strtotime($value['time']),
201
            ];
202
        }
203
        return ['Aging' => $defaultNA, 'Time' => $defaultNA, 'Time as PHP no.' => $defaultNA];
204
    }
205
206
    private function getPkgVerNo($version) {
207
        $vrs = $version;
208
        if (substr($version, 0, 1) == 'v') {
209
            $vrs = substr($version, 1, strlen($version) - 1);
210
        }
211
        if (strpos($vrs, '-') !== false) {
212
            $vrs = substr($vrs, 0, strpos($vrs, '-'));
213
        }
214
        return $vrs;
215
    }
216
217
    private function getPkgVersion($value, $defaultNA) {
218
        if (isset($value['version'])) {
219
            return [
220
                'Notification URL' => $value['notification-url'],
221
                'Version no.'      => $this->getPkgVerNo($value['version']),
222
            ];
223
        }
224
        return ['Notification URL' => $defaultNA, 'Version no.' => $defaultNA];
225
    }
226
227
}
228