1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sugarcrm\UpgradeSpec\Updater; |
4
|
|
|
|
5
|
|
|
use Sugarcrm\UpgradeSpec\Updater\Adapter\AdapterInterface; |
6
|
|
|
use Sugarcrm\UpgradeSpec\Updater\Exception\UpdaterException; |
7
|
|
|
|
8
|
|
|
class Updater |
9
|
|
|
{ |
10
|
|
|
const STABILITY_STABLE = 'stable'; |
11
|
|
|
const STABILITY_UNSTABLE = 'unstable'; |
12
|
|
|
const STABILITY_ANY = 'any'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var AdapterInterface |
16
|
|
|
*/ |
17
|
|
|
private $adapter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Updater constructor. |
21
|
|
|
* |
22
|
|
|
* @param AdapterInterface $adapter |
23
|
|
|
*/ |
24
|
|
|
public function __construct(AdapterInterface $adapter) |
25
|
|
|
{ |
26
|
|
|
$this->adapter = $adapter; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return mixed |
31
|
|
|
* |
32
|
|
|
* @throws UpdaterException |
33
|
|
|
*/ |
34
|
|
|
public function hasUpdate() |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
return $this->adapter->hasUpdate(); |
38
|
|
|
} catch (\Exception $e) { |
39
|
|
|
throw new UpdaterException($e->getMessage(), $e->getCode(), $e); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function getOldVersion() |
47
|
|
|
{ |
48
|
|
|
return $this->adapter->getOldVersion(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function getNewVersion() |
55
|
|
|
{ |
56
|
|
|
return $this->adapter->getNewVersion(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $stability |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
* |
64
|
|
|
* @throws UpdaterException |
65
|
|
|
*/ |
66
|
|
|
public function update($stability = self::STABILITY_ANY) |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
|
|
$this->validateStability($stability); |
70
|
|
|
|
71
|
|
|
return $this->adapter->update($stability); |
72
|
|
|
} catch (\Exception $e) { |
73
|
|
|
throw new UpdaterException($e->getMessage(), $e->getCode(), $e); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return mixed |
79
|
|
|
* |
80
|
|
|
* @throws UpdaterException |
81
|
|
|
*/ |
82
|
|
|
public function rollback() |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
return $this->adapter->rollback(); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
throw new UpdaterException($e->getMessage(), $e->getCode(), $e); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $stability |
93
|
|
|
*/ |
94
|
|
|
private function validateStability($stability) |
95
|
|
|
{ |
96
|
|
View Code Duplication |
if (!in_array($stability, [self::STABILITY_STABLE, self::STABILITY_UNSTABLE, self::STABILITY_ANY])) { |
|
|
|
|
97
|
|
|
throw new \InvalidArgumentException('Invalid stability'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.