Conditions | 21 |
Paths | 201 |
Total Lines | 100 |
Code Lines | 54 |
Lines | 7 |
Ratio | 7 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
45 | * @var integer |
||
46 | */ |
||
47 | private $pid; |
||
48 | |||
49 | /** |
||
50 | * Construct a new instance of the registry. |
||
51 | * |
||
52 | * @param Collection $collection The MongoDB collection containing the process information. |
||
53 | */ |
||
54 | public function __construct(Collection $collection) |
||
55 | { |
||
56 | $this->collection = $collection; |
||
57 | $this->encodedHostName = str_replace(['.', '$'], ['_DOT_', '_DOLLAR_'], gethostname()); |
||
58 | $this->pid = getmypid(); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Add to process registry. Adds based on $maxGlobalProcesses and $maxHostProcesses after a process registry |
||
63 | * cleaning. |
||
64 | * |
||
65 | * @param ProcessInterface $process The process to add. |
||
66 | * |
||
67 | * @return boolean true if the process was added, false if not or there is too much concurrency at the moment. |
||
68 | */ |
||
69 | public function add(ProcessInterface $process) : bool |
||
70 | { |
||
71 | //loop in case the update fails its optimistic concurrency check |
||
72 | for ($i = 0; $i < 5; ++$i) { |
||
73 | $this->ensureProcessDocumentExists($process->getName()); |
||
74 | $existing = $this->getExistingProcessDocument($process->getName()); |
||
75 | $replacement = $this->generateReplacementDocument($existing); |
||
76 | $totalPidCount = $this->getTotalPidCount($replacement); |
||
77 | $thisHostPids = $replacement['hosts'][$this->encodedHostName] ?? []; |
||
78 | if ($totalPidCount >= $process->getMaxGlobalProcesses() |
||
79 | || count($thisHostPids) >= $process->getMaxHostProcesses()) { |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | // add our process |
||
84 | $thisHostPids[$this->pid] = $this->getExpiresSeconds($process->getMinsBeforeExpire()); |
||
85 | $replacement['hosts'][$this->encodedHostName] = $thisHostPids; |
||
86 | |||
87 | if ($this->updateExistingDocument($existing['_id'], $existing['version'], $replacement)) { |
||
88 | return true; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | //too much concurrency at the moment, return false to signify not added. |
||
93 | return false; |
||
94 | } |
||
95 | |||
96 | private function updateExistingDocument(string $id, ObjectID $version, array $replacement) : bool |
||
97 | { |
||
98 | $status = $this->collection->replaceOne( |
||
99 | ['_id' => $id, 'version' => $version], |
||
100 | $replacement, |
||
101 | ['writeConcern' => new \MongoDB\Driver\WriteConcern(1, 100, true)] |
||
102 | ); |
||
103 | |||
104 | return $status->getMatchedCount() === 1; |
||
105 | } |
||
106 | |||
107 | private function ensureProcessDocumentExists(string $id) |
||
108 | { |
||
109 | $this->collection->findOneAndUpdate( |
||
110 | ['_id' => $id], |
||
111 | ['$setOnInsert' => ['hosts' => [], 'version' => new ObjectID()]], |
||
112 | ['upsert' => true] |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | private function generateReplacementDocument(array $existing) : array |
||
117 | { |
||
118 | $replacement = $existing; |
||
119 | $replacement['version'] = new ObjectID(); |
||
120 | |||
121 | //clean $replacement based on their pids and expire times |
||
122 | foreach ($existing['hosts'] as $hostname => $pids) { |
||
123 | $this->removeExpiredPids($pids, $hostname, $replacement); |
||
124 | if (empty($replacement['hosts'][$hostname])) { |
||
125 | unset($replacement['hosts'][$hostname]); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | return $replacement; |
||
130 | } |
||
131 | |||
132 | private function getExistingProcessDocument(string $id) : array |
||
133 | { |
||
134 | return $this->collection->findOne( |
||
135 | ['_id' => $id], |
||
136 | ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] |
||
137 | ); |
||
138 | } |
||
139 | |||
140 | private function removeExpiredPids(array $pids, string $hostname, array &$replacement) |
||
141 | { |
||
142 | foreach ($pids as $pid => $expires) { |
||
143 | if ($this->canRemovePid($hostname, $pid, $expires)) { |
||
144 | unset($replacement['hosts'][$hostname][$pid]); |
||
145 | } |
||
213 |