Completed
Push — master ( f0b491...11ee78 )
by Thomas
09:37
created

AbstractPackageInstaller::updateExtensions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 6
1
<?php
2
namespace keeko\core\installer;
3
4
use Composer\IO\IOInterface;
5
use keeko\core\model\Package;
6
use keeko\core\model\PackageQuery;
7
use keeko\core\service\ServiceContainer;
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
use keeko\core\schema\KeekoPackageSchema;
10
use keeko\core\model\ExtensionQuery;
11
use keeko\core\model\Extension;
12
use phootwork\json\Json;
13
14
abstract class AbstractPackageInstaller {
15
	
16
	/** @var ServiceContainer */
17
	protected $service;
18
	
19
	/** @var EventDispatcher */
20
	protected $dispatcher;
21
	
22
	public function __construct(ServiceContainer $service) {
23
		$this->service = $service;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
24
		$this->dispatcher = $this->service->getDispatcher();
25
	}
26
27
	/**
28
	 * Installs a package
29
	 *
30
	 * @param IOInterface $io the IOInterface
31
	 * @param string $packageName
32
	 * @throws MissingOptionsException when required parameters for the package are missing
33
	 */
34
	abstract public function install(IOInterface $io, $packageName);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
35
36
	abstract public function update(IOInterface $io, $packageName, $from, $to);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
37
38
	abstract public function uninstall(IOInterface $io, $packageName);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
39
40
	protected function updatePackage(Package &$model, KeekoPackageSchema $pkg) {
41
		$packageName = $pkg->getPackage()->getFullName();
42
		$result = PackageQuery::create()->filterByName($packageName)->count();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
43
		$info = $this->service->getPackageManager()->getComposerPackage($packageName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
44
		
45
		if ($result == 0) {
46
			$model->setTitle($pkg->getTitle());
47
			$model->setDescription($info->getDescription());
48
			$model->setName($packageName);
49
			$model->setInstalledVersion($info->getPrettyVersion());
50
			$model->save();
51
		}
52
		
53
		$this->updateExtensions($model, $pkg);
54
	}
55
	
56
	protected function getPackageSchema($packageName) {
57
		return $this->service->getPackageManager()->getPackage($packageName);
58
	}
59
	
60
	protected function updateExtensions(Package &$model, KeekoPackageSchema $pkg) {
61
		// remove all existing extensions from this package first
62
		ExtensionQuery::create()->filterByPackage($model)->deleteAll();
63
		
64
		// add them one by one
65
		foreach ($pkg->getExtensions() as $key => $data) {
66
			$ext = new Extension();
67
			$ext->setKey($key);
68
			$ext->setData(Json::encode($data));
69
			$ext->save();
70
		}
71
	}
72
}