These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This software package is licensed under AGPL or Commercial license. |
||
5 | * |
||
6 | * @package maslosoft/mangan |
||
7 | * @licence AGPL or Commercial |
||
8 | * @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
||
9 | * @copyright Copyright (c) Maslosoft |
||
10 | * @copyright Copyright (c) Others as mentioned in code |
||
11 | * @link https://maslosoft.com/mangan/ |
||
12 | */ |
||
13 | |||
14 | namespace Maslosoft\Mangan; |
||
15 | |||
16 | use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
||
17 | use Maslosoft\Mangan\Exceptions\CommandException; |
||
18 | use Maslosoft\Mangan\Exceptions\CommandNotFoundException; |
||
19 | use Maslosoft\Mangan\Helpers\CollectionNamer; |
||
20 | use Maslosoft\Mangan\Model\Command\User; |
||
21 | use Maslosoft\Mangan\Traits\AvailableCommands; |
||
22 | |||
23 | /** |
||
24 | * Command |
||
25 | * |
||
26 | * @author Piotr Maselkowski <pmaselkowski at gmail.com> |
||
27 | */ |
||
28 | class Command |
||
29 | { |
||
30 | |||
31 | use AvailableCommands; |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * @var AnnotatedInterface |
||
36 | */ |
||
37 | private $model = null; |
||
38 | |||
39 | /** |
||
40 | * |
||
41 | * @var Mangan |
||
42 | */ |
||
43 | private $mn = null; |
||
44 | |||
45 | private $collection = ''; |
||
46 | |||
47 | 14 | public function __construct(AnnotatedInterface $model = null, Mangan $mangan = null) |
|
48 | { |
||
49 | 14 | $this->model = $model; |
|
50 | 14 | if (!empty($mangan)) |
|
51 | { |
||
52 | 8 | $this->mn = $mangan; |
|
53 | } |
||
54 | 14 | if (empty($model)) |
|
55 | { |
||
56 | 4 | $this->mn = Mangan::fly(); |
|
57 | 4 | return; |
|
58 | } |
||
59 | 10 | $this->mn = Mangan::fromModel($model); |
|
60 | 10 | $this->collection = CollectionNamer::nameCollection($model); |
|
61 | 10 | } |
|
62 | |||
63 | 3 | public function call($command, $arguments = []) |
|
64 | { |
||
65 | 3 | $arg = $this->model ? CollectionNamer::nameCollection($this->model) : true; |
|
66 | 3 | $cmd = [$command => $arg]; |
|
67 | 3 | if (is_array($arguments) && count($arguments)) |
|
68 | { |
||
69 | 1 | $cmd = array_merge($cmd, $arguments); |
|
70 | } |
||
71 | 3 | $result = $this->mn->getDbInstance()->command($cmd); |
|
72 | |||
73 | 3 | if (array_key_exists('errmsg', $result) && array_key_exists('ok', $result) && $result['ok'] == 0) |
|
74 | { |
||
75 | 1 | if (array_key_exists('bad cmd', $result)) |
|
76 | { |
||
77 | $badCmd = key($result['bad cmd']); |
||
78 | if ($badCmd == $command) |
||
79 | { |
||
80 | throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); |
||
81 | } |
||
82 | } |
||
83 | 1 | elseif (strpos($result['errmsg'], 'no such command') !== false) |
|
84 | { |
||
85 | 1 | throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); |
|
86 | } |
||
87 | throw new CommandException(sprintf('Could not execute command `%s`, mongo returned: "%s"', $command, $result['errmsg'])); |
||
88 | } |
||
89 | 2 | return $result; |
|
90 | } |
||
91 | |||
92 | public function __call($name, $arguments) |
||
93 | { |
||
94 | if (count($arguments)) |
||
95 | { |
||
96 | return $this->call($name, $arguments[0]); |
||
97 | } |
||
98 | return $this->call($name); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Explicitly creates a collection or view. |
||
103 | * |
||
104 | * Parameter `$params` depends on MongoDB version, |
||
105 | * see (official documentation)[https://docs.mongodb.com/manual/reference/command/create/] for details |
||
106 | * |
||
107 | * @param string $collectionName The name of the new collection |
||
108 | * @param array $params |
||
109 | * @return array |
||
110 | */ |
||
111 | 1 | public function create($collectionName, $params = []) |
|
112 | { |
||
113 | $cmd = [ |
||
114 | 1 | 'create' => $collectionName |
|
115 | ]; |
||
116 | 1 | return $this->mn->getDbInstance()->command(array_merge($cmd, $params)); |
|
117 | } |
||
118 | |||
119 | 2 | public function createUser(User $user, $writeConcerns = []) |
|
0 ignored issues
–
show
|
|||
120 | { |
||
121 | $cmd = [ |
||
122 | 2 | 'createUser' => $user->user, |
|
123 | 2 | 'pwd' => $user->pwd, |
|
124 | ]; |
||
125 | 2 | if (!empty($user->customData)) |
|
126 | { |
||
127 | assert(is_object($user->customData)); |
||
128 | $cmd['customData'] = $user->customData; |
||
129 | } |
||
130 | 2 | $cmd = array_merge($cmd, $user->toArray(['user', 'customData'])); |
|
131 | 2 | return $this->mn->getDbInstance()->command($cmd); |
|
132 | } |
||
133 | |||
134 | 2 | public function dropUser($username, $writeConcerns = []) |
|
135 | { |
||
136 | 2 | if ($username instanceof User) |
|
137 | { |
||
138 | 2 | $username = $username->user; |
|
139 | } |
||
140 | $cmd = [ |
||
141 | 2 | 'dropUser' => $username |
|
142 | ]; |
||
143 | 2 | return $this->mn->getDbInstance()->command(array_merge($cmd, $writeConcerns)); |
|
144 | } |
||
145 | |||
146 | 8 | public function createIndex($keys, $options = []) |
|
147 | { |
||
148 | // Ensure array |
||
149 | 8 | if(empty($options)) |
|
150 | { |
||
151 | 7 | $options = []; |
|
152 | } |
||
153 | 8 | return $this->mn->getDbInstance()->selectCollection($this->collection)->createIndex($keys, $options); |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * NOTE: This is broken |
||
158 | * @return array |
||
159 | * @throws Exceptions\ManganException |
||
160 | */ |
||
161 | public function getIndexes() |
||
162 | { |
||
163 | return $this->mn->getDbInstance()->selectCollection($this->collection)->getIndexInfo(); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * The `collStats` command returns a variety of storage statistics for a given collection. |
||
168 | * |
||
169 | * @param string $collectionName The name of the target collection. If the collection does not exist, collStats returns an error message. |
||
170 | * @param int $scale Optional. The scale used in the output to display the sizes of items. By default, output displays sizes in bytes. To display kilobytes rather than bytes, specify a scale value of 1024. The scale factor rounds values to whole numbers. |
||
171 | * @param boolean $verbose Optional. When true, collStats increases reporting for the MMAPv1 Storage Engine. Defaults to false. |
||
172 | * @return array |
||
173 | */ |
||
174 | 9 | public function collStats($collectionName, $scale = 1, $verbose = false) |
|
175 | { |
||
176 | $cmd = [ |
||
177 | 9 | 'collStats' => $collectionName, |
|
178 | 9 | 'scale' => $scale, |
|
179 | 9 | 'verbose' => $verbose |
|
180 | ]; |
||
181 | 9 | return $this->mn->getDbInstance()->command($cmd); |
|
182 | } |
||
183 | |||
184 | } |
||
185 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.