Completed
Push — master ( 2c5f1b...de26ae )
by Peter
05:39
created

Command::createUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2.0625
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 6
	public function __construct(AnnotatedInterface $model = null, Mangan $mangan = null)
46
	{
47 6
		$this->model = $model;
48 6
		if (!empty($mangan))
49
		{
50
			$this->mn = $mangan;
51
		}
52 6
		if (empty($model))
53
		{
54 4
			$this->mn = Mangan::fly();
55 4
			return;
56
		}
57 2
		$this->mn = Mangan::fromModel($model);
58 2
	}
59
60 3
	public function call($command, $arguments = [])
61
	{
62 3
		$arg = $this->model ? CollectionNamer::nameCollection($this->model) : true;
63 3
		$cmd = [$command => $arg];
64 3
		if (is_array($arguments) && count($arguments))
65
		{
66 1
			$cmd = array_merge($cmd, $arguments);
67
		}
68 3
		$result = $this->mn->getDbInstance()->command($cmd);
69
70 3
		if (array_key_exists('errmsg', $result) && array_key_exists('ok', $result) && $result['ok'] == 0)
71
		{
72 1
			if (array_key_exists('bad cmd', $result))
73
			{
74
				$badCmd = key($result['bad cmd']);
75
				if ($badCmd == $command)
76
				{
77
					throw new CommandNotFoundException(sprintf('Command `%s` not found', $command));
78
				}
79
			}
80 1
			elseif (strpos($result['errmsg'], 'no such command') !== false)
81
			{
82 1
				throw new CommandNotFoundException(sprintf('Command `%s` not found', $command));
83
			}
84
			throw new CommandException(sprintf('Could not execute command `%s`, mongo returned: "%s"', $command, $result['errmsg']));
85
		}
86 2
		return $result;
87
	}
88
89
	public function __call($name, $arguments)
90
	{
91
		if (count($arguments))
92
		{
93
			return $this->call($name, $arguments[0]);
94
		}
95
		return $this->call($name);
96
	}
97
98
	/**
99
	 * Explicitly creates a collection or view.
100
	 *
101
	 * Parameter `$params` depends on MongoDB version,
102
	 * see (official documentation)[https://docs.mongodb.com/manual/reference/command/create/] for details
103
	 *
104
	 * @param string $collectionName The name of the new collection
105
	 * @param array $params
106
	 * @return array
107
	 */
108 1
	public function create($collectionName, $params = [])
109
	{
110
		$cmd = [
111 1
			'create' => $collectionName
112
		];
113 1
		return $this->mn->getDbInstance()->command(array_merge($cmd, $params));
114
	}
115
116 2
	public function createUser(User $user, $writeConcerns = [])
0 ignored issues
show
Unused Code introduced by
The parameter $writeConcerns is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
	{
118
		$cmd = [
119 2
			'createUser' => $user->user,
120 2
			'pwd' => $user->pwd,
121
		];
122 2
		if (!empty($user->customData))
123
		{
124
			assert(is_object($user->customData));
125
			$cmd['customData'] = $user->customData;
126
		}
127 2
		$cmd = array_merge($cmd, $user->toArray(['user', 'customData']));
128 2
		return $this->mn->getDbInstance()->command($cmd);
129
	}
130
131 2
	public function dropUser($username, $writeConcerns = [])
132
	{
133 2
		if ($username instanceof User)
134
		{
135 2
			$username = $username->user;
136
		}
137
		$cmd = [
138 2
			'dropUser' => $username
139
		];
140 2
		return $this->mn->getDbInstance()->command(array_merge($cmd, $writeConcerns));
141
	}
142
143
	/**
144
	 * The `collStats` command returns a variety of storage statistics for a given collection.
145
	 *
146
	 * @param string $collectionName The name of the target collection. If the collection does not exist, collStats returns an error message.
147
	 * @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.
148
	 * @param boolean $verbose Optional. When true, collStats increases reporting for the MMAPv1 Storage Engine. Defaults to false.
149
	 * @return array
150
	 */
151 1
	public function collStats($collectionName, $scale = 1, $verbose = false)
152
	{
153
		$cmd = [
154 1
			'collStats' => $collectionName,
155 1
			'scale' => $scale,
156 1
			'verbose' => $verbose
157
		];
158 1
		return $this->mn->getDbInstance()->command($cmd);
159
	}
160
161
}
162