Skip to content

Commit 1d25e1a

Browse files
committed
Fix #78, restructuring
1 parent 6f8c2be commit 1d25e1a

File tree

13 files changed

+151
-133
lines changed

13 files changed

+151
-133
lines changed

.codacy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude_paths:
2+
- 'tests/*'

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_style = space
11+
indent_size = 4
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

src/AbstractRepositoryType.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Codedge\Updater\Events\HasWrongPermissions;
88
use Exception;
9-
use GuzzleHttp\Client;
9+
use GuzzleHttp\ClientInterface;
1010
use Illuminate\Support\Facades\File;
1111
use Symfony\Component\Finder\Finder;
1212

@@ -44,7 +44,7 @@ abstract class AbstractRepositoryType
4444
*
4545
* @return bool
4646
*/
47-
protected function unzipArchive($file = '', $targetDir = '', $deleteZipArchive = true): bool
47+
protected function unzipArchive($file, $targetDir, $deleteZipArchive = true): bool
4848
{
4949
if (empty($file) || ! File::exists($file)) {
5050
throw new \InvalidArgumentException("Archive [{$file}] cannot be found or is empty.");
@@ -99,13 +99,14 @@ protected function hasCorrectPermissionForUpdate(): bool
9999
/**
100100
* Download a file to a given location.
101101
*
102-
* @param Client $client
102+
* @param ClientInterface $client
103103
* @param string $source Url for the source (.zip)
104104
* @param string $storagePath
105105
*
106106
* @return mixed|\Psr\Http\Message\ResponseInterface
107+
* @throws \GuzzleHttp\Exception\GuzzleException
107108
*/
108-
protected function downloadRelease(Client $client, string $source, $storagePath)
109+
protected function downloadRelease(ClientInterface $client, string $source, $storagePath)
109110
{
110111
$headers = [];
111112

src/SourceRepositoryTypes/GithubRepositoryType.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,27 @@ class GithubRepositoryType extends AbstractRepositoryType
3535
/**
3636
* Github constructor.
3737
*
38-
* @param Client $client
3938
* @param array $config
4039
*/
41-
public function __construct(Client $client, array $config)
40+
public function __construct(array $config)
4241
{
43-
$this->client = $client;
4442
$this->config = $config;
45-
$this->config['version_installed'] = config('self-update.version_installed');
46-
$this->config['exclude_folders'] = config('self-update.exclude_folders');
4743
}
4844

4945
public function create(): GithubRepositoryTypeContract
5046
{
5147
$this->checkValidRepository();
5248

5349
if ($this->useBranchForVersions()) {
54-
return new GithubBranchType($this->config, $this->client);
50+
return resolve(GithubBranchType::class);
5551
}
5652

57-
return new GithubTagType($this->config, $this->client);
53+
return resolve(GithubTagType::class);
5854
}
5955

6056
public function update(string $version = ''): bool
6157
{
62-
$this->setPathToUpdate(base_path(), $this->config['exclude_folders']);
58+
$this->setPathToUpdate(base_path(), config('self-update.exclude_folders'));
6359

6460
if ($this->hasCorrectPermissionForUpdate()) {
6561
if (! empty($version)) {
@@ -70,13 +66,13 @@ public function update(string $version = ''): bool
7066

7167
// Move all directories first
7268
collect((new Finder())->in($sourcePath)
73-
->exclude($this->config['exclude_folders'])
69+
->exclude(config('self-update.exclude_folders'))
7470
->directories()
7571
->sort(function ($a, $b) {
7672
return strlen($b->getRealpath()) - strlen($a->getRealpath());
7773
}))->each(function (/** @var \SplFileInfo $directory */ $directory) {
7874
if (! $this->isDirectoryExcluded(
79-
File::directories($directory->getRealPath()), $this->config['exclude_folders'])
75+
File::directories($directory->getRealPath()), config('self-update.exclude_folders'))
8076
) {
8177
File::copyDirectory(
8278
$directory->getRealPath(),
@@ -129,6 +125,6 @@ protected function checkValidRepository(): void
129125
*/
130126
public function getVersionInstalled(string $prepend = '', string $append = ''): string
131127
{
132-
return $prepend.$this->config['version_installed'].$append;
128+
return $prepend.config('self-update.version_installed').$append;
133129
}
134130
}

src/SourceRepositoryTypes/GithubRepositoryTypes/GithubBranchType.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Codedge\Updater\Contracts\GithubRepositoryTypeContract;
88
use Codedge\Updater\Events\UpdateAvailable;
99
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryType;
10-
use GuzzleHttp\Client;
10+
use GuzzleHttp\ClientInterface;
1111
use Illuminate\Support\Collection;
1212
use Illuminate\Support\Facades\File;
1313
use Illuminate\Support\Facades\Log;
@@ -18,18 +18,13 @@
1818
final class GithubBranchType extends GithubRepositoryType implements GithubRepositoryTypeContract
1919
{
2020
/**
21-
* @var array
22-
*/
23-
protected $config;
24-
25-
/**
26-
* @var Client
21+
* @var ClientInterface
2722
*/
2823
protected $client;
2924

30-
public function __construct(array $config, Client $client)
25+
public function __construct(array $config, ClientInterface $client)
3126
{
32-
$this->config = $config;
27+
parent::__construct($config);
3328
$this->setAccessToken($config['private_access_token']);
3429

3530
$this->client = $client;
@@ -125,8 +120,7 @@ public function getVersionAvailable(string $prepend = '', string $append = ''):
125120

126121
protected function getRepositoryReleases(): ResponseInterface
127122
{
128-
$url = self::GITHUB_API_URL
129-
.DIRECTORY_SEPARATOR.'repos'
123+
$url = DIRECTORY_SEPARATOR.'repos'
130124
.DIRECTORY_SEPARATOR.$this->config['repository_vendor']
131125
.DIRECTORY_SEPARATOR.$this->config['repository_name']
132126
.DIRECTORY_SEPARATOR.'commits'

src/SourceRepositoryTypes/GithubRepositoryTypes/GithubTagType.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Codedge\Updater\Events\UpdateAvailable;
99
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryType;
1010
use Exception;
11-
use GuzzleHttp\Client;
11+
use GuzzleHttp\ClientInterface;
1212
use Illuminate\Support\Facades\File;
1313
use Illuminate\Support\Str;
1414
use InvalidArgumentException;
@@ -17,18 +17,13 @@
1717
final class GithubTagType extends GithubRepositoryType implements GithubRepositoryTypeContract
1818
{
1919
/**
20-
* @var array
21-
*/
22-
protected $config;
23-
24-
/**
25-
* @var Client
20+
* @var ClientInterface
2621
*/
2722
protected $client;
2823

29-
public function __construct(array $config, Client $client)
24+
public function __construct(array $config, ClientInterface $client)
3025
{
31-
$this->config = $config;
26+
parent::__construct($config);
3227
$this->setAccessToken($config['private_access_token']);
3328

3429
$this->client = $client;
@@ -130,8 +125,7 @@ public function fetch($version = ''): void
130125

131126
protected function getRepositoryReleases(): ResponseInterface
132127
{
133-
$url = self::GITHUB_API_URL
134-
.'/repos/'.$this->config['repository_vendor']
128+
$url = '/repos/'.$this->config['repository_vendor']
135129
.'/'.$this->config['repository_name']
136130
.'/tags';
137131

src/UpdaterManager.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public function __construct(Application $app)
5353
*
5454
* @param string $name
5555
*
56-
* @return SourceRepository
56+
* @return SourceRepositoryTypeContract
5757
*/
58-
public function source(string $name = ''): SourceRepository
58+
public function source(string $name = ''): SourceRepositoryTypeContract
5959
{
6060
$name = $name ?: $this->getDefaultSourceRepository();
6161

@@ -161,38 +161,32 @@ protected function resolve(string $name)
161161
$repositoryMethod = 'create'.ucfirst($name).'Repository';
162162

163163
if (method_exists($this, $repositoryMethod)) {
164-
return $this->{$repositoryMethod}($config);
164+
return $this->{$repositoryMethod}();
165165
}
166166
throw new InvalidArgumentException("Repository [{$name}] is not supported.");
167167
}
168168

169169
/**
170170
* Create an instance for the Github source repository.
171171
*
172-
* @param array $config
173-
*
174172
* @return SourceRepository
175173
*/
176-
protected function createGithubRepository(array $config): SourceRepository
174+
protected function createGithubRepository(): SourceRepository
177175
{
178-
$client = new Client();
179-
$factory = new GithubRepositoryType($client, $config);
176+
/** @var GithubRepositoryType $factory */
177+
$factory = $this->app->make(GithubRepositoryType::class);
180178

181179
return $this->sourceRepository($factory->create());
182180
}
183181

184182
/**
185183
* Create an instance for the Http source repository.
186184
*
187-
* @param array $config
188-
*
189185
* @return SourceRepository
190186
*/
191-
protected function createHttpRepository(array $config): SourceRepository
187+
protected function createHttpRepository(): SourceRepository
192188
{
193-
$client = new Client();
194-
195-
return $this->sourceRepository(new HttpRepositoryType($client, $config));
189+
return $this->sourceRepository($this->app->make(HttpRepositoryType::class));
196190
}
197191

198192
/**

src/UpdaterServiceProvider.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
namespace Codedge\Updater;
44

55
use Codedge\Updater\Commands\CheckForUpdate;
6+
use Codedge\Updater\Contracts\GithubRepositoryTypeContract;
7+
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryType;
8+
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryTypes\GithubBranchType;
9+
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryTypes\GithubTagType;
10+
use Codedge\Updater\SourceRepositoryTypes\HttpRepositoryType;
11+
use GuzzleHttp\Client;
612
use Illuminate\Contracts\Container\Container;
713
use Illuminate\Support\ServiceProvider;
814

@@ -78,9 +84,30 @@ protected function registerCommands()
7884
*/
7985
protected function registerManager()
8086
{
81-
$this->app->singleton('updater', function (Container $app) {
82-
return new UpdaterManager($app);
87+
$this->app->singleton('updater', function () {
88+
return new UpdaterManager(app());
8389
});
90+
91+
$this->app->bind(GithubRepositoryType::class, function(): GithubRepositoryType {
92+
return new GithubRepositoryType(config('self-update.repository_types.github'));
93+
});
94+
95+
$this->app->bind(GithubBranchType::class, function(): GithubRepositoryTypeContract {
96+
$client = new Client(['base_url' => GithubRepositoryTypeContract::GITHUB_URL]);
97+
98+
return new GithubBranchType(config('self-update.repository_types.github'), $client);
99+
});
100+
101+
$this->app->bind(GithubTagType::class, function(): GithubRepositoryTypeContract {
102+
$client = new Client(['base_url' => GithubRepositoryTypeContract::GITHUB_API_URL]);
103+
104+
return new GithubTagType(config('self-update.repository_types.github'), $client);
105+
});
106+
107+
$this->app->bind(HttpRepositoryType::class, function() {
108+
return new HttpRepositoryType(new Client(), config('self-update.repository_types.http'));
109+
});
110+
84111
$this->app->alias('updater', UpdaterManager::class);
85112
}
86113

tests/SourceRepositoryTypes/GithubRepositoryTypeTest.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,27 @@
88
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryTypes\GithubBranchType;
99
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryTypes\GithubTagType;
1010
use Codedge\Updater\Tests\TestCase;
11-
use GuzzleHttp\Client;
1211
use Codedge\Updater\Contracts\GithubRepositoryTypeContract;
12+
use Illuminate\Foundation\Testing\Concerns\InteractsWithContainer;
1313
use InvalidArgumentException;
1414
use Exception;
1515

1616
class GithubRepositoryTypeTest extends TestCase
1717
{
18-
/**
19-
* @var Client;
20-
*/
21-
protected $client;
22-
23-
/**
24-
* @var array
25-
*/
26-
protected $config;
27-
28-
public function setUp(): void
29-
{
30-
parent::setUp();
31-
$this->config = $this->app['config']['self-update']['repository_types']['github'];
32-
$this->client = $this->getMockedClient('tag');
33-
}
34-
3518
public function testCreateGithubTagTypeInstance()
3619
{
3720
/** @var GithubTagType $github */
38-
$github = (new GithubRepositoryType($this->client, $this->config))->create();
21+
$github = (resolve(GithubRepositoryType::class))->create();
3922

4023
$this->assertInstanceOf(GithubTagType::class, $github);
41-
4224
}
4325

4426
public function testCreateGithubBranchTypeInstance()
4527
{
46-
$this->config['use_branch'] = 'v2';
28+
config(['self-update.repository_types.github.use_branch' => 'v2']);
4729

4830
/** @var GithubBranchType $github */
49-
$github = (new GithubRepositoryType($this->client, $this->config))->create();
31+
$github = (resolve(GithubRepositoryType::class))->create();
5032

5133
$this->assertInstanceOf(GithubBranchType::class, $github);
5234
}

0 commit comments

Comments
 (0)