Completed
Push — master ( 224027...f8147e )
by Daniel
02:04
created

ComposerPackagesListing::getPkgTiming()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 2
eloc 7
nc 2
nop 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
     * Returns a complete list of packages and respective details from a composer.lock file
41
     *
42
     * @param string $fileToRead
43
     * @return array
44
     */
45
    protected function getPackageDetailsFromGivenComposerLockFile($fileToRead)
46
    {
47
        if (!file_exists($fileToRead)) {
48
            return ['error' => $fileToRead . ' was not found'];
49
        }
50
        $dNA      = '---';
51
        $alnfo    = [];
52
        $packages = $this->getPkgFileInListOfPackageArrayOut($fileToRead);
53
        foreach ($packages['packages'] as $value) {
54
            $basic                 = $this->getPkgBasicInfo($value, $dNA);
55
            $atr                   = $this->getPkgOptAtributeAll($value, $dNA);
56
            $alnfo[$value['name']] = array_merge($basic, $atr, $this->getPkgTiming($value, $dNA));
57
            ksort($alnfo[$value['name']]);
58
        }
59
        ksort($alnfo);
60
        return $alnfo;
61
    }
62
63
    private function getPkgAging($timePkg)
64
    {
65
        $dateTimeToday = new \DateTime(date('Y-m-d', strtotime('today')));
66
        $dateTime      = new \DateTime(date('Y-m-d', strtotime($timePkg)));
67
        $interval      = $dateTimeToday->diff($dateTime);
68
        return $interval->format('%a days ago');
69
    }
70
71
    private function getPkgBasicInfo($value, $defaultNA)
72
    {
73
        return [
74
            'License'          => (isset($value['license']) ? $this->getPkgLcns($value['license']) : $defaultNA),
75
            'Notification URL' => (isset($value['version']) ? $value['notification-url'] : $defaultNA),
76
            'Package Name'     => $value['name'],
77
            'PHP required'     => (isset($value['require']['php']) ? $value['require']['php'] : $defaultNA),
78
            'Product'          => explode('/', $value['name'])[1],
79
            'Vendor'           => explode('/', $value['name'])[0],
80
            'Version no.'      => (isset($value['version']) ? $this->getPkgVerNo($value['version']) : $defaultNA),
81
        ];
82
    }
83
84
    private function getPkgFileInListOfPackageArrayOut($fileToRead)
85
    {
86
        $handle       = fopen($fileToRead, 'r');
87
        $fileContents = fread($handle, filesize($fileToRead));
88
        fclose($handle);
89
        return json_decode($fileContents, true);
90
    }
91
92
    private function getPkgLcns($license)
93
    {
94
        $lcns = $license;
95
        if (is_array($license)) {
96
            $lcns = implode(', ', $license);
97
        }
98
        return $lcns;
99
    }
100
101
    private function getPkgOptAtributeAll($value, $defaultNA)
102
    {
103
        $attr    = ['description', 'homepage', 'type', 'url', 'version'];
104
        $aReturn = [];
105
        foreach ($attr as $valueA) {
106
            $aReturn[ucwords($valueA)] = $defaultNA;
107
            if (array_key_exists($valueA, $value)) {
108
                $aReturn[ucwords($valueA)] = $value[$valueA];
109
            }
110
        }
111
        return $aReturn;
112
    }
113
114
    private function getPkgTiming($value, $defaultNA)
115
    {
116
        if (isset($value['time'])) {
117
            return [
118
                'Aging'           => $this->getPkgAging($value['time']),
119
                'Time'            => date('l, d F Y H:i:s', strtotime($value['time'])),
120
                'Time as PHP no.' => strtotime($value['time']),
121
            ];
122
        }
123
        return ['Aging' => $defaultNA, 'Time' => $defaultNA, 'Time as PHP no.' => $defaultNA];
124
    }
125
126
    private function getPkgVerNo($version)
127
    {
128
        $vrs = $version;
129
        if (substr($version, 0, 1) == 'v') {
130
            $vrs = substr($version, 1, strlen($version) - 1);
131
        }
132
        if (strpos($vrs, '-') !== false) {
133
            $vrs = substr($vrs, 0, strpos($vrs, '-'));
134
        }
135
        return $vrs;
136
    }
137
}
138