FileParser::parse()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 26
rs 8.8333
cc 7
nc 6
nop 1
1
<?php
2
3
/**
4
 * Author: mickael Louzet @micklouzet
5
 * File: FileParser.php
6
 * Created: 05/12/2019
7
 */
8
9
declare(strict_types=1);
10
11
namespace ComposerLockParser\Parser;
12
13
use ComposerLockParser\Package\Package;
14
use ComposerLockParser\Package\PackageCollection;
15
use ComposerLockParser\Exception\FileNotFoundException;
16
use ComposerLockParser\Exception\InvalidComposerLockFileException;
17
18
class FileParser implements FileParserInterface
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23
    public static function parse(string $filePath): PackageCollection
24
    {
25
        if (strcmp(pathinfo($filePath)['basename'], 'composer.lock') !== 0) {
26
            throw new InvalidComposerLockFileException();
27
        }
28
29
        if (false === \file_exists($filePath)) {
30
            throw new FileNotFoundException();
31
        }
32
33
        $data = \json_decode(\file_get_contents($filePath), true, 512, JSON_THROW_ON_ERROR);
34
35
        if (empty($data)) {
36
            return new PackageCollection();
37
        }
38
39
        $packages = new PackageCollection();
40
41
        if (!$data || !isset($data['packages'])) {
42
            return new PackageCollection();
43
        }
44
45
        foreach ($data['packages'] as $package) {
46
            $packages[] = Package::factory($package);
47
        }
48
        return $packages;
49
    }
50
}
51