1
|
|
|
<?php |
2
|
|
|
namespace SeleniumSetup\Locker; |
3
|
|
|
|
4
|
|
|
use SeleniumSetup\FileSystem; |
5
|
|
|
use SeleniumSetup\SeleniumSetup; |
6
|
|
|
|
7
|
|
|
class Locker |
8
|
|
|
{ |
9
|
|
|
protected $fileSystem; |
10
|
|
|
/** |
11
|
|
|
* @var ServerItem[] |
12
|
|
|
*/ |
13
|
|
|
protected $locker = []; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$this->fileSystem = new FileSystem(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function openLockFile() |
21
|
|
|
{ |
22
|
|
|
if (!$this->fileSystem->isFile(SeleniumSetup::$APP_ROOT_PATH . DIRECTORY_SEPARATOR . SeleniumSetup::DEFAULT_LOCK_FILENAME)) { |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$contents = $this->fileSystem->readFile(SeleniumSetup::$APP_ROOT_PATH . DIRECTORY_SEPARATOR . SeleniumSetup::DEFAULT_LOCK_FILENAME); |
27
|
|
|
$lockerRaw = json_decode($contents); |
28
|
|
|
foreach ($lockerRaw as $serverObj) { |
29
|
|
|
$this->addServer(ServerItemFactory::createFromObj($serverObj)); |
30
|
|
|
} |
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function writeToLockFile() |
35
|
|
|
{ |
36
|
|
|
$this->fileSystem->writeToFile( |
37
|
|
|
SeleniumSetup::$APP_ROOT_PATH . DIRECTORY_SEPARATOR . SeleniumSetup::DEFAULT_LOCK_FILENAME, |
38
|
|
|
$this->toJson() |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getServer($name) |
45
|
|
|
{ |
46
|
|
|
if (!isset($this->locker[$name])) { |
47
|
|
|
throw new \Exception('Unknown server.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->locker[$name]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getServers() |
54
|
|
|
{ |
55
|
|
|
return $this->locker; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function addServer(ServerItem $server) |
59
|
|
|
{ |
60
|
|
|
$this->locker[$server->getName()] = $server; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function emptyLocker() |
64
|
|
|
{ |
65
|
|
|
$this->locker = []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function toArray() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$result = []; |
71
|
|
|
foreach ($this->locker as $server) { |
72
|
|
|
$result[$server->getName()] = $server->toArray(); |
73
|
|
|
} |
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function toJson() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$result = []; |
80
|
|
|
foreach ($this->locker as $server) { |
81
|
|
|
$result[$server->getName()] = $server->toArray(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return json_encode($result, JSON_PRETTY_PRINT); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
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.