1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\CommandBus\Handler; |
4
|
|
|
|
5
|
|
|
use Buttress\Concrete\Service\Package\Package; |
6
|
|
|
use Buttress\Concrete\Service\Package\PackageItem; |
7
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\Install; |
8
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\ListPackages; |
9
|
|
|
use Buttress\Concrete\CommandBus\Command\Package\Uninstall; |
10
|
|
|
use Buttress\Concrete\Exception\RuntimeException; |
11
|
|
|
|
12
|
|
|
class PackageHandler |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** @var \Buttress\Concrete\Service\Package\Package */ |
16
|
|
|
private $package; |
17
|
|
|
|
18
|
|
|
public function __construct(Package $package) |
19
|
|
|
{ |
20
|
|
|
$this->package = $package; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
View Code Duplication |
public function handleInstall(Install $command) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$result = $this->package->install((new PackageItem())->setHandle($command->getHandle())); |
26
|
|
|
if (!$result->success()) { |
27
|
|
|
$errors = $result->getErrors(); |
28
|
|
|
throw new RuntimeException(reset($errors)); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function handleUninstall(Uninstall $command) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$result = $this->package->uninstall((new PackageItem())->setHandle($command->getHandle())); |
35
|
|
|
if (!$result->success()) { |
36
|
|
|
$errors = $result->getErrors(); |
37
|
|
|
throw new RuntimeException(reset($errors)); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function handleListPackages(ListPackages $command) |
42
|
|
|
{ |
43
|
|
|
$packages = iterator_to_array($this->package->all()); |
44
|
|
|
usort($packages, function(PackageItem $a, PackageItem $b) { |
|
|
|
|
45
|
|
|
return $a->isInstalled() ? 1 : -1; |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
if ($table = array_map([$this, 'listPackage'], $packages)) { |
49
|
|
|
$command->getCli()->table($table); |
50
|
|
|
} else { |
51
|
|
|
$command->getCli()->out('No packages found.'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function listPackage(PackageItem $item) |
56
|
|
|
{ |
57
|
|
|
if ($item->isInstalled()) { |
58
|
|
|
$installed = sprintf('<green>%s</green>', $item->getInstalledVersion()); |
59
|
|
|
} else { |
60
|
|
|
$installed = '<yellow>No</yellow>'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return [ |
64
|
|
|
'Package Handle' => $item->getHandle(), |
65
|
|
|
'Version' => $item->getVersion(), |
66
|
|
|
'Installed' => $installed, |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.