ParserService::hydrateModelFromRequest()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 2
1
<?php
2
namespace NOSQL\Services;
3
4
use MongoDB\Client;
5
use NOSQL\Api\base\NOSQLBase;
6
use NOSQL\Dto\Model\NOSQLModelDto;
7
use NOSQL\Models\base\NOSQLModelTrait;
8
use NOSQL\Models\NOSQLActiveRecord;
9
use PSFS\base\config\Config;
10
use PSFS\base\exception\ApiException;
11
use PSFS\base\Singleton;
12
13
/**
14
 * Class ParserService
15
 * @package NOSQL\Services
16
 */
17
final class ParserService extends  Singleton {
18
    /**
19
     * @param $domain
20
     * @return \MongoDB\Database
21
     */
22
    public function createConnection($domain) {
23
        $lowerDomain = strtolower($domain);
24
        $protocol = Config::getParam('nosql.protocol', 'mongodb');
25
        $dns = $protocol . '://';
26
        $dns .= Config::getParam('nosql.user', '', $lowerDomain);
27
        $dns .= ':' . Config::getParam('nosql.password', '', $lowerDomain);
28
        $dns .= '@' . Config::getParam('nosql.host', 'localhost', $lowerDomain);
29
30
        $database = Config::getParam('nosql.database', 'nosql', $lowerDomain);
31
        if(null !== Config::getParam('nosql.replicaset')) {
32
            $dns .= '/' . $database . '?ssl=true&replicaSet=' . Config::getParam('nosql.replicaset', null, $lowerDomain);
33
            $dns .= '&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000';
34
        } else {
35
            if(strtolower($protocol) !== 'mongodb+srv') {
0 ignored issues
show
Bug introduced by
It seems like $protocol can also be of type null; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            if(strtolower(/** @scrutinizer ignore-type */ $protocol) !== 'mongodb+srv') {
Loading history...
36
                $dns .= ':' . Config::getParam('nosql.port', '27017', $lowerDomain);
37
            }
38
            $dns .= '/' . $database . "?authSource=admin";
39
        }
40
        $dns .= '&retryWrites=true&w=majority';
41
        $client = new Client($dns);
42
        return $client->selectDatabase($database);
43
    }
44
45
    /**
46
     * @param string $domain
47
     * @param $collection
48
     * @param NOSQLModelDto $dto
49
     * @throws ApiException
50
     * @throws \NOSQL\Exceptions\NOSQLValidationException
51
     * @throws \ReflectionException
52
     */
53
    public function checkAndSave($domain, $collection, NOSQLModelDto $dto) {
0 ignored issues
show
Unused Code introduced by
The parameter $domain is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public function checkAndSave(/** @scrutinizer ignore-unused */ $domain, $collection, NOSQLModelDto $dto) {

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

Loading history...
Unused Code introduced by
The parameter $collection is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public function checkAndSave($domain, /** @scrutinizer ignore-unused */ $collection, NOSQLModelDto $dto) {

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

Loading history...
54
        $errors = $dto->validate();
55
        if(empty($errors)) {
56
57
        } else {
58
            throw new ApiException(t('Dto not valid'), 400);
59
        }
60
    }
61
62
    /**
63
     * @param array $data
64
     * @param $className
65
     * @return NOSQLModelTrait|null
66
     * @throws \NOSQL\Exceptions\NOSQLValidationException
67
     * @throws \ReflectionException
68
     */
69
    public function hydrateModelFromRequest(array $data, $className) {
70
        $model = null;
71
        $reflectionClass = new \ReflectionClass($className);
72
        if($reflectionClass->isSubclassOf(NOSQLBase::class)) {
73
            /** @var NOSQLActiveRecord $modelName */
74
            $modelName = $className::MODEL_CLASS;
75
            $model = $modelName::fromArray($data);
76
        }
77
        return $model;
78
    }
79
}
80