Completed
Push — master ( 957a77...d1fc9e )
by Fran
02:04
created

NOSQLQuery   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 31
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findPk() 0 11 2
A find() 0 19 3
A parseCriteria() 0 12 3
1
<?php
2
namespace NOSQL\Models;
3
4
use MongoDB\BSON\ObjectId;
5
use MongoDB\Database;
6
use NOSQL\Dto\Model\ResultsetDto;
7
use NOSQL\Models\base\NOSQLParserTrait;
8
use PSFS\base\config\Config;
9
use PSFS\base\exception\ApiException;
10
use PSFS\base\types\Api;
11
12
final class NOSQLQuery {
13
    /**
14
     * @param $pk
15
     * @param Database|null $con
16
     * @return mixed
17
     * @throws ApiException
18
     */
19
    public static function findPk($modelName, $pk, Database $con = null) {
20
        $model = new $modelName();
21
        $con = NOSQLParserTrait::initConnection($model, $con);
22
        $collection = $con->selectCollection($model->getSchema()->name);
23
        $result = $collection->findOne(['_id' => new ObjectId($pk)]);
24
        if(null !== $result) {
25
            $model->feed($result->getArrayCopy());
26
        } else {
27
            throw new ApiException(t('Document not found'), 404);
28
        }
29
        return $model;
30
    }
31
32
    /**
33
     * @param string $modelName
34
     * @param array $criteria
35
     * @param Database|null $con
36
     * @return ResultsetDto
37
     * @throws \NOSQL\Exceptions\NOSQLValidationException
38
     * @throws \PSFS\base\exception\GeneratorException
39
     */
40
    public static function find($modelName, array $criteria, Database $con = null) {
41
        /** @var NOSQLActiveRecord $model */
42
        $model = new $modelName();
43
        $con = NOSQLParserTrait::initConnection($model, $con);
44
        $collection = $con->selectCollection($model->getSchema()->name);
45
        $resultSet = new ResultsetDto(false);
46
        $resultSet->count = $collection->countDocuments($filters);
47
        $nosqlOptions = [
48
            'limit' => (integer)(array_key_exists(Api::API_LIMIT_FIELD, $criteria) ? $criteria[Api::API_LIMIT_FIELD] : Config::getParam('pagination.limit', 50)),
49
        ];
50
        $filters = self::parseCriteria($criteria, $model);
51
        $results = $collection->find($filters, $nosqlOptions);
52
        /** @var  $result */
53
        $items = $results->toArray();
54
        foreach($items as $item) {
55
            $model->feed($item->getArrayCopy(), true);
56
            $resultSet->items[] = $model->getDtoCopy(true);
57
        }
58
        return $resultSet;
59
    }
60
61
    /**
62
     * @param array $criteria
63
     * @param NOSQLActiveRecord $model
64
     * @return array
65
     */
66
    private static function parseCriteria(array $criteria, NOSQLActiveRecord $model)
67
    {
68
        $filters = [];
69
        foreach ($model->getSchema()->properties as $property) {
70
            if (array_key_exists($property->name, $criteria)) {
71
                $filters[$property->name] = [
72
                    '$regex' => $criteria[$property->name],
73
                    '$options' => 'i',
74
                ];
75
            }
76
        }
77
        return $filters;
78
    }
79
}