1
|
|
|
<?php namespace Comodojo\Extender\Socket; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Daemon\Process; |
4
|
|
|
use \Comodojo\Extender\Worklog\Manager; |
5
|
|
|
use \Comodojo\Extender\Worklog\Transformer; |
6
|
|
|
use \Comodojo\Foundation\Utils\ArrayOps; |
7
|
|
|
use \League\Fractal\Manager as FractalManager; |
8
|
|
|
use \League\Fractal\Resource\Item; |
9
|
|
|
use \League\Fractal\Resource\Collection; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @package Comodojo Extender |
13
|
|
|
* @author Marco Giovinazzi <[email protected]> |
14
|
|
|
* @license MIT |
15
|
|
|
* |
16
|
|
|
* LICENSE: |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
class WorklogCommands { |
28
|
|
|
|
29
|
|
|
public function count(Process $daemon) { |
30
|
|
|
|
31
|
|
|
return self::getManager($daemon)->count(); |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function list(Process $daemon, array $payload = null) { |
36
|
|
|
|
37
|
|
|
if ( empty($payload) ) { |
38
|
|
|
|
39
|
|
|
$data = self::getManager($daemon)->get(); |
40
|
|
|
|
41
|
|
|
} else { |
42
|
|
|
|
43
|
|
|
$options = ArrayOps::replaceStrict([ |
44
|
|
|
'limit' => 10, |
45
|
|
|
'offset' => 0, |
46
|
|
|
'reverse' => false |
47
|
|
|
], $payload); |
48
|
|
|
|
49
|
|
|
$data = self::getManager($daemon)->get( |
50
|
|
|
[], |
51
|
|
|
$options['limit'], |
52
|
|
|
$options['offset'], |
53
|
|
|
$options['reverse'] |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$resource = new Collection($data, new Transformer); |
59
|
|
|
$fractal = new FractalManager(); |
60
|
|
|
$data = $fractal->createData($resource)->toArray(); |
61
|
|
|
|
62
|
|
|
return $data['data']; |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function byId(Process $daemon, $id) { |
|
|
|
|
67
|
|
|
|
68
|
|
|
$data = self::getManager($daemon)->getOne(['id' => $id]); |
69
|
|
|
|
70
|
|
|
if ( empty($data) ) return null; |
71
|
|
|
|
72
|
|
|
$resource = new Item($data, new Transformer); |
73
|
|
|
$fractal = new FractalManager(); |
74
|
|
|
$data = $fractal->createData($resource)->toArray(); |
75
|
|
|
|
76
|
|
|
return $data['data']; |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function byJid(Process $daemon, $jid) { |
|
|
|
|
81
|
|
|
|
82
|
|
|
$data = self::getManager($daemon)->get(['jid' => $jid]); |
83
|
|
|
|
84
|
|
|
$resource = new Collection($data, new Transformer); |
85
|
|
|
$fractal = new FractalManager(); |
86
|
|
|
$data = $fractal->createData($resource)->toArray(); |
87
|
|
|
|
88
|
|
|
return $data['data']; |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
public function byUid(Process $daemon, $uid) { |
|
|
|
|
93
|
|
|
|
94
|
|
|
$data = self::getManager($daemon)->getOne(['uid' => $uid]); |
95
|
|
|
|
96
|
|
|
if ( empty($data) ) return null; |
97
|
|
|
|
98
|
|
|
$resource = new Item($data, new Transformer); |
99
|
|
|
$fractal = new FractalManager(); |
100
|
|
|
$data = $fractal->createData($resource)->toArray(); |
101
|
|
|
|
102
|
|
|
return $data['data']; |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function byPid(Process $daemon, $pid) { |
|
|
|
|
107
|
|
|
|
108
|
|
|
$data = self::getManager($daemon)->get(['pid' => $pid]); |
109
|
|
|
|
110
|
|
|
$resource = new Collection($data, new Transformer); |
111
|
|
|
$fractal = new FractalManager(); |
112
|
|
|
$data = $fractal->createData($resource)->toArray(); |
113
|
|
|
|
114
|
|
|
return $data['data']; |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected static function getManager(Process $daemon) { |
119
|
|
|
|
120
|
|
|
return new Manager( |
121
|
|
|
$daemon->getConfiguration(), |
|
|
|
|
122
|
|
|
$daemon->getLogger(), |
123
|
|
|
$daemon->getEvents() |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
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.