psfs /
nosql
| 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
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
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
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...
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
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 |