Completed
Pull Request — master (#4)
by Jakub
03:36
created

JsonTools::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Json;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
use Zalas\Toolbox\Json\Factory\ToolFactory;
8
use Zalas\Toolbox\Tool\Collection;
9
use Zalas\Toolbox\Tool\Command\ShCommand;
10
use Zalas\Toolbox\Tool\Command\TestCommand;
11
use Zalas\Toolbox\Tool\Tool;
12
use Zalas\Toolbox\Tool\Tools;
13
14
final class JsonTools implements Tools
15
{
16
    /**
17
     * @var string
18
     */
19
    private $resource;
20
21 10
    public function __construct(string $resource)
22
    {
23 10
        if (!\is_readable($resource)) {
24 1
            throw new InvalidArgumentException(\sprintf('Could not read the file: "%s".', $resource));
25
        }
26
27 9
        $this->resource = $resource;
28
    }
29
30
    /**
31
     * @return Collection|Tool[]
32
     */
33 5
    public function all(): Collection
34
    {
35 5
        return $this->defaultTools()
36 5
            ->merge(Collection::create(
37 5
                \array_map(\sprintf('%s::import', ToolFactory::class), $this->loadJson())
38
            ));
39
    }
40
41 5
    private function loadJson(): array
42
    {
43 5
        $json = \json_decode(\file_get_contents($this->resource), true);
44
45 5
        if (!$json) {
46 1
            throw new RuntimeException(\sprintf('Failed to parse json: "%s"', $this->resource));
47
        }
48
49 4
        if (!isset($json['tools']) || !\is_array($json['tools'])) {
50 2
            throw new RuntimeException(\sprintf('Failed to find any tools in: "%s".', $this->resource));
51
        }
52
53 2
        return $json['tools'];
54
    }
55
56 5
    private function defaultTools(): Collection
57
    {
58 5
        return Collection::create([
59 5
            new Tool(
60 5
                'composer',
61 5
                'Dependency Manager for PHP',
62 5
                'https://getcomposer.org/',
63 5
                new ShCommand('composer self-update'),
64 5
                new TestCommand('composer list', 'composer')
65
            ),
66 5
            new Tool(
67 5
                'composer-bin-plugin',
68 5
                'Composer plugin to install bin vendors in isolated locations',
69 5
                'https://github.com/bamarni/composer-bin-plugin',
70 5
                new ShCommand('composer global require bamarni/composer-bin-plugin'),
71 5
                new TestCommand('composer global show bamarni/composer-bin-plugin', 'composer-bin-plugin')
72
            ),
73 5
            new Tool(
74 5
                'box',
75 5
                'An application for building and managing Phars',
76 5
                'https://box-project.github.io/box2/',
77 5
                new ShCommand('curl -Ls https://box-project.github.io/box2/installer.php | php && mv box.phar /usr/local/bin/box && chmod +x /usr/local/bin/box'),
78 5
                new TestCommand('box list', 'box')
79
            ),
80
        ]);
81
    }
82
}
83