Completed
Pull Request — master (#21)
by Robbie
01:41
created

ComposerLoader::getJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace BringYourOwnIdeas\UpdateChecker;
4
5
use Exception;
6
7
/**
8
 * The composer loader class is responsible for dealing directly with composer.json and composer.lock files,
9
 * in terms of loading and parsing their contents.
10
 *
11
 * Any requirements for dealing with these files directly should use this class as a proxy.
12
 */
13
class ComposerLoader
14
{
15
    /**
16
     * @var object
17
     */
18
    protected $json;
19
20
    /**
21
     * @var object
22
     */
23
    protected $lock;
24
25
    /**
26
     * @var string
27
     */
28
    protected $basePath;
29
30
    /**
31
     * @param string $basePath
32
     * @throws Exception
33
     */
34
    public function __construct($basePath = '')
35
    {
36
        if ($basePath) {
37
            $this->setBasePath($basePath);
38
        }
39
        $this->build();
40
    }
41
42
    /**
43
     * Load and build the composer.json and composer.lock files
44
     *
45
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be ComposerLoader|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
46
     * @throws Exception If either file could not be loaded
47
     */
48
    public function build()
49
    {
50
        $basePath = $this->getBasePath();
51
        $composerJson = file_get_contents($basePath . '/composer.json');
52
        $composerLock = file_get_contents($basePath . '/composer.lock');
53
54
        if (!$composerJson || !$composerLock) {
55
            throw new Exception('composer.json or composer.lock could not be found!');
56
        }
57
58
        $this->setJson(json_decode($composerJson));
59
        $this->setLock(json_decode($composerLock));
60
    }
61
62
    /**
63
     * @param object $json
64
     * @return ComposerLoader
65
     */
66
    public function setJson($json)
67
    {
68
        $this->json = $json;
69
        return $this;
70
    }
71
72
    /**
73
     * @return object
74
     */
75
    public function getJson()
76
    {
77
        return $this->json;
78
    }
79
80
    /**
81
     * @param object $lock
82
     * @return ComposerLoader
83
     */
84
    public function setLock($lock)
85
    {
86
        $this->lock = $lock;
87
        return $this;
88
    }
89
90
    /**
91
     * @return object
92
     */
93
    public function getLock()
94
    {
95
        return $this->lock;
96
    }
97
98
    /**
99
     * Set the base path, if not specified the default will be `BASE_PATH`
100
     *
101
     * @param string $basePath
102
     * @return $this
103
     */
104
    public function setBasePath($basePath)
105
    {
106
        $this->basePath = $basePath;
107
        return $this;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getBasePath()
114
    {
115
        return $this->basePath ?: BASE_PATH;
116
    }
117
}
118