| 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 |  |  | 	 * @var AnnotatedInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	private $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @var Mangan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	private $mn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * @var string  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	private $collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 13 |  | 	public function __construct(AnnotatedInterface $model = null, Mangan $mangan = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 13 |  | 		$this->model = $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 13 |  | 		if ($mangan !== null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 8 |  | 			$this->mn = $mangan; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 13 |  | 		if ($model === null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  | 			$this->mn = Mangan::fly(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 9 |  | 		$this->mn = Mangan::fromModel($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 9 |  | 		$this->collection = CollectionNamer::nameCollection($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 9 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  | 	public function call($command, $arguments = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  | 		$arg = $this->model ? CollectionNamer::nameCollection($this->model) : true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  | 		$cmd = [$command => $arg]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  | 		if (is_array($arguments) && count($arguments)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			$cmd = array_merge($cmd, $arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  | 		$result = $this->mn->getDbInstance()->command($cmd); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  | 		if (array_key_exists('errmsg', $result) && array_key_exists('ok', $result) && $result['ok'] == 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			if (array_key_exists('bad cmd', $result)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 				$badCmd = key($result['bad cmd']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 				if ($badCmd == $command) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 				{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 					throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			elseif (strpos($result['errmsg'], 'no such command') !== false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 				throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			throw new CommandException(sprintf('Could not execute command `%s`, mongo returned: "%s"', $command, $result['errmsg'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	public function __call($name, $arguments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		if (count($arguments)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			return $this->call($name, $arguments[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		return $this->call($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * Explicitly creates a collection or view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * Parameter `$params` depends on MongoDB version, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * see (official documentation)[https://docs.mongodb.com/manual/reference/command/create/] for details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * @param string $collectionName The name of the new collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  | 	public function create($collectionName, $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$cmd = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  | 			'create' => $collectionName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  | 		return $this->mn->getDbInstance()->command(array_merge($cmd, $params)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  | 	public function createUser(User $user, $writeConcerns = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$cmd = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  | 			'createUser' => $user->user, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  | 			'pwd' => $user->pwd, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 2 |  | 		if (!empty($user->customData)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			assert(is_object($user->customData)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			$cmd['customData'] = $user->customData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 2 |  | 		$cmd = array_merge($cmd, $user->toArray(['user', 'customData'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  | 		return $this->mn->getDbInstance()->command($cmd); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 2 |  | 	public function dropUser($username, $writeConcerns = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 2 |  | 		if ($username instanceof User) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 2 |  | 			$username = $username->user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		$cmd = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 2 |  | 			'dropUser' => $username | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  | 		return $this->mn->getDbInstance()->command(array_merge($cmd, $writeConcerns)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 8 |  | 	public function createIndex($keys, $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		// Ensure array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 8 |  | 		if(empty($options)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 7 |  | 			$options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 8 |  | 		return $this->mn->getDbInstance()->selectCollection($this->collection)->createIndex($keys, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * NOTE: This is broken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * @throws Exceptions\ManganException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 161 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  | 	public function getIndexes() | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  | 		return $this->mn->getDbInstance()->selectCollection($this->collection)->getIndexInfo(); | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	 * The `collStats` command returns a variety of storage statistics for a given collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	 * @param string $collectionName The name of the target collection. If the collection does not exist, collStats returns an error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	 * @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. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	 * @param boolean $verbose Optional. When true, collStats increases reporting for the MMAPv1 Storage Engine. Defaults to false. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 9 |  | 	public function collStats($collectionName, $scale = 1, $verbose = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		$cmd = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 9 |  | 			'collStats' => $collectionName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 9 |  | 			'scale' => $scale, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 9 |  | 			'verbose' => $verbose | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 9 |  | 		return $this->mn->getDbInstance()->command($cmd); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 185 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 186 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.