GithubStrategy::downloadLatestVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @package: marx/php-self-updater
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-12-27
7
 *
8
 */
9
10
namespace PSU\Strategy;
11
12
use PSU\Exception\StrategyException;
13
use PSU\HttpClient\HttpClientFactory;
14
15
class GithubStrategy implements StrategyInterface
16
{
17
    const API_URL = 'https://api.github.com/repos/%s/%s/releases/latest';
18
19
    /**
20
     * @var int
21
     */
22
    private $stability = StrategyInterface::STABILITY_STABLE;
23
24
    /**
25
     * @var string
26
     */
27
    private $githubOwner = '';
28
29
    /**
30
     * @var string
31
     */
32
    private $githubRepo = '';
33
34
    /**
35
     * @var string
36
     */
37
    private $pharFile = '';
38
39
    /**
40
     * @var HttpClientFactory
41
     */
42
    private $httpClientFactory;
43
44
    /**
45
     * @var array
46
     */
47
    private $lastResponse = [];
48
49
    /**
50
     * @param HttpClientFactory $httpClientFactory
51
     */
52 3
    public function __construct(
53
        HttpClientFactory $httpClientFactory
54
    )
55
    {
56 3
        $this->httpClientFactory = $httpClientFactory;
57 3
    }
58
59
    /**
60
     * @return string
61
     * @throws StrategyException
62
     */
63 2
    public function getLatestVersion()
64
    {
65 2
        $jsonResponse = $this->getReleaseInfo();
66
67
        if (
68 2
            StrategyInterface::STABILITY_STABLE == $this->stability
69 2
            && isset($jsonResponse['prerelease'])
70 2
            && true == $jsonResponse['prerelease']
71 1
        )
72 2
        {
73
            //todo : get latest stable release
74
            return '0.0.0';
75
        }
76
77 2
        return (isset($jsonResponse['tag_name']))
78 2
            ? str_replace('v', '', $jsonResponse['tag_name'])
79 2
            : '0.0.0';
80
    }
81
82
    /**
83
     *
84
     */
85 1
    public function downloadLatestVersion()
86
    {
87 1
        return $this->getHttpClient()->download(
88 1
            $this->getPharDownloadUrl()
89 1
        );
90
    }
91
92
    /**
93
     * @param int $stability
94
     */
95
    public function setStability($stability)
96
    {
97
        $this->stability = $stability;
98
    }
99
100
    /**
101
     * @param $pharFileName
102
     */
103 1
    public function setPharFile($pharFileName)
104
    {
105 1
        $this->pharFile = $pharFileName;
106 1
    }
107
108
    /**
109
     * @param string $owner
110
     */
111 3
    public function setGithubOwner($owner)
112
    {
113 3
        $this->githubOwner = $owner;
114 3
    }
115
116
    /**
117
     * @param string $repo
118
     */
119 3
    public function setGithubRepo($repo)
120
    {
121 3
        $this->githubRepo = $repo;
122 3
    }
123
124
    /**
125
     * @return \PSU\HttpClient\HttpClientInterface
126
     */
127 3
    private function getHttpClient()
128
    {
129 3
        return $this->httpClientFactory->getHttpClient();
130
    }
131
132
    /**
133
     * @return string
134
     * @throws StrategyException
135
     */
136 3
    private function getApiCallUrl()
137
    {
138 3
        if (empty($this->githubOwner) || empty($this->githubRepo))
139 3
        {
140
            throw new StrategyException(
141
                '"githubOwner" or "githubRepo" is empty. Please set the owner and repository first.',
142
                StrategyException::ERROR_MISSING_PARAMETER
143
            );
144
        }
145
146 3
        return sprintf(
147 3
            self::API_URL,
148 3
            $this->githubOwner,
149 3
            $this->githubRepo
150 3
        );
151
    }
152
153
    /**
154
     * @return array
155
     * @throws StrategyException
156
     */
157 3
    private function getReleaseInfo()
158
    {
159 3
        if (!empty($this->lastResponse))
160 3
        {
161
            return $this->lastResponse;
162
        }
163
164 3
        return $this->lastResponse = $this->getHttpClient()->getJsonResponse(
165 3
            $this->getApiCallUrl()
166 3
        );
167
    }
168
169
    /**
170
     * @return string
171
     */
172 1
    private function getPharDownloadUrl()
173
    {
174 1
        $jsonResponse = $this->getReleaseInfo();
175
176 1
        foreach ($jsonResponse['assets'] as $asset)
177
        {
178 1
            if ($asset['name'] == $this->pharFile) //todo: add correct filter
179 1
            {
180 1
                return $asset['browser_download_url'];
181
            }
182
        }
183
184
        return '';
185
    }
186
}