Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
90.91% |
10 / 11 |
CRAP | |
96.23% |
51 / 53 |
| Executor | |
0.00% |
0 / 1 |
|
90.91% |
10 / 11 |
22 | |
96.23% |
51 / 53 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getParser | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setParser | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| fromJs | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| withData | |
100.00% |
1 / 1 |
4 | |
100.00% |
7 / 7 |
|||
| clearData | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| execute | |
100.00% |
1 / 1 |
5 | |
100.00% |
11 / 11 |
|||
| raw | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| getMongoCollection | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| createMongoCollection | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
| setConnectionConfig | |
100.00% |
1 / 1 |
4 | |
100.00% |
11 / 11 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\MongoJsExecutor; |
| 4 | |
| 5 | use Exception; |
| 6 | use Illuminate\Database\Eloquent\Collection; |
| 7 | use Jenssegers\Mongodb\Eloquent\Model; |
| 8 | use Qmp\Laravel\DBConnector\Config; |
| 9 | use Qmp\Laravel\DBConnector\Connectors\MongoConnector; |
| 10 | use Qmp\Laravel\MongoJsExecutor\Exceptions\ConnectionNotFound; |
| 11 | use Qmp\Laravel\MongoJsExecutor\Exceptions\MissingValueArgument; |
| 12 | |
| 13 | class Executor |
| 14 | { |
| 15 | /** |
| 16 | * Undocumented variable |
| 17 | * |
| 18 | * @var [type] |
| 19 | */ |
| 20 | protected $model; |
| 21 | |
| 22 | /** |
| 23 | * Undocumented variable |
| 24 | * |
| 25 | * @var MongoJsParser |
| 26 | */ |
| 27 | protected $parser; |
| 28 | |
| 29 | /** |
| 30 | * Undocumented variable |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | protected $connection; |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Undocumented function |
| 39 | * |
| 40 | * @param Model $model |
| 41 | */ |
| 42 | public function __construct(Model $model = null) |
| 43 | { |
| 44 | $this->model = $model; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Undocumented function |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | public function getParser() |
| 53 | { |
| 54 | return $this->parser; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Undocumented function |
| 59 | * |
| 60 | * @param Parser $parser |
| 61 | * @return void |
| 62 | */ |
| 63 | public function setParser(Parser $parser) |
| 64 | { |
| 65 | $this->parser = $parser; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Undocumented function |
| 70 | * |
| 71 | * @param string $filepath |
| 72 | * @param [type] $disk |
| 73 | * @return Executpr |
| 74 | */ |
| 75 | public static function fromJs($filepath, $collectionSuffix = null, $disk = null) |
| 76 | { |
| 77 | $executor = new self(); |
| 78 | $parser = new Parser($collectionSuffix); |
| 79 | $parser->fromFile($filepath, $disk); |
| 80 | |
| 81 | $executor->setParser($parser); |
| 82 | |
| 83 | return $executor; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Undocumented function |
| 88 | * |
| 89 | * @param string $key |
| 90 | * @param [type] $value |
| 91 | * @return self |
| 92 | */ |
| 93 | public function withData($key, $value = null): self |
| 94 | { |
| 95 | if (is_array($key)) { |
| 96 | foreach ($key as $k => $v) { |
| 97 | $this->parser->withData($k, $v); |
| 98 | } |
| 99 | } else { |
| 100 | if (!$value) { |
| 101 | throw new MissingValueArgument(); |
| 102 | } |
| 103 | $this->parser->withData($key, $value); |
| 104 | } |
| 105 | |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Undocumented function |
| 111 | * |
| 112 | * @return self |
| 113 | */ |
| 114 | public function clearData(): self |
| 115 | { |
| 116 | $this->parser->clearData(); |
| 117 | return $this; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * Undocumented function |
| 123 | * |
| 124 | * @param array $options |
| 125 | * @return Collection |
| 126 | */ |
| 127 | public function execute($options = [], $collection = null) |
| 128 | { |
| 129 | if (!$this->parser->isParsed()) { |
| 130 | $this->parser->parse(); |
| 131 | |
| 132 | if (!$this->model) { |
| 133 | $this->setConnectionConfig($this->parser->getDatabase()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | $raw = $this->raw($options); |
| 138 | |
| 139 | if (is_a($raw, \MongoDB\Driver\Cursor::class)) { |
| 140 | $collection = collect(); |
| 141 | |
| 142 | foreach ($raw as $entry) { |
| 143 | $collection->push(collect(iterator_to_array($entry))); |
| 144 | } |
| 145 | |
| 146 | return $collection; |
| 147 | } |
| 148 | |
| 149 | return $raw; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Undocumented function |
| 154 | * |
| 155 | * @param [type] $options |
| 156 | * @return \Traversable |
| 157 | */ |
| 158 | protected function raw($options) |
| 159 | { |
| 160 | $command = $this->parser->getCommand(); |
| 161 | $query = $this->parser->getParsedJs(); |
| 162 | |
| 163 | return $this->getMongoCollection()->$command($query, $options); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Undocumented function |
| 168 | * |
| 169 | * @return \MongoDB\Collection |
| 170 | */ |
| 171 | protected function getMongoCollection() |
| 172 | { |
| 173 | if ($this->model) { |
| 174 | return $this->model::raw(); |
| 175 | } else { |
| 176 | return $this->createMongoCollection(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Undocumented function |
| 182 | * |
| 183 | * @return void |
| 184 | */ |
| 185 | protected function createMongoCollection() |
| 186 | { |
| 187 | $connector = new MongoConnector(); |
| 188 | $config = new Config($this->connection); |
| 189 | $connector->connection($config, [ |
| 190 | 'uriOptions' => [ |
| 191 | 'socketTimeoutMS' => 3600000, |
| 192 | 'connectTimeoutMS' => 3600000 |
| 193 | ] |
| 194 | ]); |
| 195 | $connector->collection($this->parser->getCollection()); |
| 196 | |
| 197 | return $connector; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Undocumented function |
| 202 | * |
| 203 | * @param [type] $database |
| 204 | * @return void |
| 205 | */ |
| 206 | protected function setConnectionConfig($database) |
| 207 | { |
| 208 | $config = array_filter(config('database.connections'), function ($conn) use ($database) { |
| 209 | if (array_key_exists('database', $conn) && $conn['database'] === $database) { |
| 210 | return true; |
| 211 | } |
| 212 | return false; |
| 213 | }); |
| 214 | |
| 215 | if (!count($config)) { |
| 216 | throw new ConnectionNotFound($database); |
| 217 | } |
| 218 | |
| 219 | $key = array_keys($config)[0]; |
| 220 | |
| 221 | $this->connection = $config[$key]; |
| 222 | $this->connection['user'] = $this->connection['username']; |
| 223 | } |
| 224 | } |