PackageJson::read()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php namespace Arcanedev\Composer\Entities;
2
3
use Arcanedev\Composer\Exceptions\InvalidPackageException;
4
use Composer\Json\JsonFile;
5
use Composer\Package\CompletePackage;
6
use Composer\Package\Loader\ArrayLoader;
7
8
/**
9
 * Class     PackageJson
10
 *
11
 * @package  Arcanedev\Composer\Entities
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class PackageJson
15
{
16
    /* -----------------------------------------------------------------
17
     |  Main Methods
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Read the contents of a composer.json style file into an array.
23
     *
24
     * The package contents are fixed up to be usable to create a Package object
25
     * by providing dummy "name" and "version" values if they have not been provided in the file.
26
     * This is consistent with the default root package loading behavior of Composer.
27
     *
28
     * @param  string  $path
29
     *
30
     * @return array
31
     */
32 99
    public static function read($path)
33
    {
34 99
        $file = new JsonFile($path);
35 99
        $json = $file->read();
36
37 99
        if ( ! isset($json['name'])) {
38 93
            $json['name'] = 'merge-plugin/'.strtr($path, DIRECTORY_SEPARATOR, '-');
39
        }
40
41 99
        if ( ! isset($json['version'])) {
42 99
            $json['version'] = '1.0.0';
43
        }
44
45 99
        return $json;
46
    }
47
48
    /**
49
     * Convert json to Package.
50
     *
51
     * @param  array  $json
52
     *
53
     * @return \Composer\Package\CompletePackage
54
     *
55
     * @throws \Arcanedev\Composer\Exceptions\InvalidPackageException
56
     */
57 99
    public static function convert(array $json)
58
    {
59 99
        $loader  = new ArrayLoader;
60 99
        $package = $loader->load($json);
61
62
63 99
        if ($package instanceof CompletePackage) {
64 99
            return $package;
65
        }
66
67
        // @codeCoverageIgnoreStart
68
        throw new InvalidPackageException(
69
            'Expected instance of CompletePackage, got '.get_class($package)
70
        );
71
        // @codeCoverageIgnoreEnd
72
    }
73
}
74