Passed
Push — master ( 784814...e0d1a0 )
by Pol
02:21
created

Composer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 104
ccs 26
cts 27
cp 0.963
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getExtra() 0 3 1
A hasType() 0 3 1
A getConfig() 0 5 1
A setWorkingDir() 0 5 1
A hasName() 0 3 1
A getComposer() 0 7 2
A getType() 0 3 2
A getVendor() 0 3 2
A getName() 0 3 2
A getProject() 0 3 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Services;
6
7
/**
8
 * Parse composer package information.
9
 */
10
final class Composer
11
{
12
    /**
13
     * @var string
14
     */
15
    private $workingDir;
16
17
    /**
18
     * Composer constructor.
19
     *
20
     * @param string $workingDir
21
     */
22 1
    public function __construct($workingDir)
23
    {
24 1
        $this->workingDir = $workingDir;
25 1
    }
26
27
    /**
28
     * @return mixed
29
     */
30 1
    public function getComposer()
31
    {
32 1
        if (false !== $content = \file_get_contents($this->workingDir . '/composer.json')) {
33 1
            return \json_decode($content, true);
34
        }
35
36
        return [];
37
    }
38
39
    /**
40
     * @param string $configKey
41
     *
42
     * @return mixed
43
     */
44 1
    public function getConfig($configKey)
45
    {
46 1
        $composer = $this->getComposer() + ['config' => []];
47
48 1
        return $composer['config'][$configKey] ?? null;
49
    }
50
51 1
    public function getExtra()
52
    {
53 1
        return $this->getComposer()['extra'] ?? [];
54
    }
55
56
    /**
57
     * @return string
58
     */
59 1
    public function getName()
60
    {
61 1
        return $this->hasName() ? $this->getComposer()['name'] : '';
62
    }
63
64
    /**
65
     * @return string
66
     */
67 1
    public function getProject()
68
    {
69 1
        return $this->hasName() ? \explode('/', $this->getName())[1] : '';
70
    }
71
72
    /**
73
     * @return string
74
     */
75 1
    public function getType()
76
    {
77 1
        return $this->hasType() ? $this->getComposer()['type'] : '';
78
    }
79
80
    /**
81
     * @return string
82
     */
83 1
    public function getVendor()
84
    {
85 1
        return $this->hasName() ? \explode('/', $this->getName())[0] : '';
86
    }
87
88
    /**
89
     * @return bool
90
     */
91 1
    public function hasName()
92
    {
93 1
        return isset($this->getComposer()['name']);
94
    }
95
96
    /**
97
     * @return bool
98
     */
99 1
    public function hasType()
100
    {
101 1
        return isset($this->getComposer()->type);
102
    }
103
104
    /**
105
     * @param string $workingDir
106
     *
107
     * @return Composer
108
     */
109 1
    public function setWorkingDir($workingDir)
110
    {
111 1
        $this->workingDir = $workingDir;
112
113 1
        return $this;
114
    }
115
}
116