Registry::analyzePackage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of Packy.
5
 *
6
 * (c) Peter Nijssen
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AppBundle\PackageManager;
13
14
use AppBundle\Entity\Package;
15
use GuzzleHttp\Client as GuzzleClient;
16
17 View Code Duplication
class Registry implements PackageManager
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    /**
20
     * @var string
21
     */
22
    private $packageVendor = 'https://registry.npmjs.org/';
23
24
    /**
25
     * @param Package $package
26
     *
27
     * @return Package
28
     */
29
    public function analyzePackage(Package $package)
30
    {
31
        $client = new GuzzleClient();
32
        $response = $client->get(
33
            $this->packageVendor . $package->getName(),
34
            [
35
                'exceptions' => false,
36
            ]
37
        );
38
39
        if ($response->getStatusCode() == 200) {
40
            $data = $this->parseJson((string) $response->getBody());
41
42
            $newestVersion = $data['dist-tags']['latest'];
43
            $package->setLatestVersion($newestVersion);
44
            $package->setLastCheckAt(new \DateTime());
45
        }
46
47
        return $package;
48
    }
49
50
    /**
51
     * Parse JSON data.
52
     *
53
     * @param string $data
54
     *
55
     * @return mixed
56
     */
57
    private function parseJson($data)
58
    {
59
        $parsedData = json_decode($data, true);
60
        if ($parsedData === false) {
61
            throw new \RuntimeException('Unable to parse json file');
62
        }
63
64
        return $parsedData;
65
    }
66
}
67