Passed
Push — master ( 3f95f4...2f7c15 )
by Mickael
06:15 queued 11s
created

FileParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
c 1
b 0
f 0
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 26 7
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\FileContentExeption;
16
use ComposerLockParser\Exception\FileNotFoundException;
17
18
class FileParser implements FileParserInterface
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23
    public static function parse(string $filePath): PackageCollection
24
    {
25
        if (!\is_file($filePath)) {
26
            throw new FileNotFoundException('Sorry! This file was not found or does not exist');
27
        }
28
29
        if (false === $content = \file_get_contents($filePath)) {
30
            throw new FileContentExeption('An error has occured while reading your file !');
31
        }
32
33
        $data = \json_decode($content, 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