From 07c2a3cc166f16440170918013460ffbc42db93a Mon Sep 17 00:00:00 2001 From: Salem Ouerdani Date: Sat, 1 Jun 2019 00:59:02 +0100 Subject: [PATCH] [Style] maybe a bit of more readable code --- src/Action.php | 14 +++++++++----- src/CreateAction.php | 34 ++++++++++++++++++++------------ src/IndexAction.php | 3 ++- src/LinkAction.php | 40 +++++++++++++++++--------------------- src/UnlinkAction.php | 12 +++++++----- src/UnlinkAllAction.php | 4 ++-- src/UrlRule.php | 43 +++++++++++++++++++++++++---------------- 7 files changed, 86 insertions(+), 64 deletions(-) diff --git a/src/Action.php b/src/Action.php index 8ee7e6f..a2ed766 100644 --- a/src/Action.php +++ b/src/Action.php @@ -49,8 +49,9 @@ public function init() parent::init(); $params = Yii::$app->request->queryParams; - if ($this->expectedParams($params) === false) + if ($this->expectedParams($params) === false) { throw new InvalidConfigException("unexpected configurations."); + } $this->relativeClass = $params['relativeClass']; $this->relationName = $params['relationName']; @@ -66,8 +67,9 @@ protected function expectedParams($params) { $expected = ['relativeClass', 'relationName', 'linkAttribute']; foreach ($expected as $attr) { - if (isset($params[$attr]) === false) return false; - if ($attr === 'linkAttribute' && isset($params[$params[$attr]]) === false) return false; + if (isset($params[$attr]) === false || ($attr === 'linkAttribute' && isset($params[$params[$attr]]) === false)) { + return false; + } } return true; } @@ -82,11 +84,13 @@ public function getRelativeModel() $relativeClass = $this->relativeClass; $relModel = $relativeClass::findOne($this->relative_id); - if ($relModel === null) + if ($relModel === null) { throw new NotFoundHttpException(StringHelper::basename($relativeClass) . " '$this->relative_id' not found."); + } - if ($this->checkAccess) + if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id, $relModel); + } return $relModel; } diff --git a/src/CreateAction.php b/src/CreateAction.php index 674fcbc..d7e71ef 100755 --- a/src/CreateAction.php +++ b/src/CreateAction.php @@ -42,8 +42,9 @@ class CreateAction extends Action */ public function run() { - if ($this->checkAccess) + if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id); + } $relModel = $this->getRelativeModel(); $relType = $relModel->getRelation($this->relationName); @@ -61,21 +62,25 @@ public function run() $model_attributes = $model->safeAttributes(); $junction = []; foreach ($viaData as $key => $value) { - if (!in_array($key, $model_attributes)) $junction[$key] = $value; + if (!in_array($key, $model_attributes)) { + $junction[$key] = $value; + } } $viaData = $junction; } $model->load($bodyParams, ''); - if ($model->save() === false && !$model->hasErrors()) + if ($model->save() === false && !$model->hasErrors()) { throw new ServerErrorHttpException('Failed to update the object for unknown reason.'); - else if ($model->hasErrors()) return $model; + } else if ($model->hasErrors()) { + return $model; + } $id = implode(',', array_values($model->getPrimaryKey(true))); if ($isManyToMany) { - $extraColumns = $viaData === null ? [] : $viaData ; + $extraColumns = $viaData === null ? [] : $viaData; if ($isManyToMany_viaClass) { $viaRelation = $relType->via[1]; @@ -84,8 +89,9 @@ public function run() $viaModel = new $viaClass; $viaModel->scenario = $this->viaScenario; - if ($this->checkAccess) + if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id, $viaModel); + } $modelClass = $this->modelClass; $pk = $modelClass::primaryKey()[0]; @@ -93,21 +99,25 @@ public function run() $attributes = array_merge([ $this->linkAttribute => $this->relative_id, $relType->link[$pk] => $id - ],$extraColumns); + ], $extraColumns); $viaModel->load($attributes, ''); - if ($viaModel->save() === false && !$viaModel->hasErrors()) + if ($viaModel->save() === false && !$viaModel->hasErrors()) { throw new ServerErrorHttpException('Failed to update the object for unknown reason.'); - else if ($viaModel->hasErrors()) return $viaModel; + } else if ($viaModel->hasErrors()) { + return $viaModel; + } + } else { + $relModel->link($this->relationName, $model, $extraColumns); } - else $relModel->link($this->relationName, $model, $extraColumns); + } else { + $relModel->link($this->relationName, $model); } - else $relModel->link($this->relationName, $model); $response = Yii::$app->getResponse(); $response->setStatusCode(201); - $response->getHeaders()->set('Location', Url::to('',true) . '/' . $id); + $response->getHeaders()->set('Location', Url::to('', true) . '/' . $id); return $model; } diff --git a/src/IndexAction.php b/src/IndexAction.php index 3a830b5..fe8e4aa 100755 --- a/src/IndexAction.php +++ b/src/IndexAction.php @@ -22,8 +22,9 @@ class IndexAction extends Action */ public function run() { - if ($this->checkAccess) + if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id); + } $relModel = $this->getRelativeModel(); $getter = 'get' . $this->relationName; diff --git a/src/LinkAction.php b/src/LinkAction.php index c63a03b..a4b336e 100755 --- a/src/LinkAction.php +++ b/src/LinkAction.php @@ -56,10 +56,9 @@ public function run($IDs) if ($linked === false) { $exist = $modelClass::find()->andWhere([$pk => $pk_value])->exists(); - - if ($exist === false) + if ($exist === false) { throw new NotFoundHttpException(StringHelper::basename($modelClass) . " '$pk_value' not found."); - + } $to_link[] = $isManyToMany_viaClass ? $pk_value : $this->findModel($pk_value); } } @@ -69,18 +68,17 @@ public function run($IDs) $viaRelation = $relType->via[1]; $viaClass = $viaRelation->modelClass; - if (count($to_link) === 0 && count($bodyParams)===0) + if (count($to_link) === 0 && count($bodyParams)===0) { Yii::$app->getResponse()->setStatusCode(304); - - else { + } else { foreach ($ids as $pk_value) { - if (in_array($pk_value, $to_link)) { $viaModel = new $viaClass; $viaModel->scenario = $this->viaScenario; - if ($this->checkAccess) + if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id, $viaModel); + } $attributes = array_merge([ $this->linkAttribute => $this->relative_id, @@ -88,26 +86,26 @@ public function run($IDs) ],$bodyParams); $viaModel->load($attributes, ''); - } - - else { + } else { // already linked -> update data in junction table. $viaModel = $viaClass::findOne([ $this->linkAttribute => $this->relative_id, $relType->link[$pk] => $pk_value ]); - if ($this->checkAccess) + if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id, $viaModel); + } $viaModel->scenario = $this->viaScenario; $viaModel->load($bodyParams, ''); } - if ($viaModel->save() === false && !$viaModel->hasErrors()) + if ($viaModel->save() === false && !$viaModel->hasErrors()) { throw new ServerErrorHttpException('Failed to update the object for unknown reason.'); - - else if ($viaModel->hasErrors()) return $viaModel; + } else if ($viaModel->hasErrors()) { + return $viaModel; + } } Yii::$app->getResponse()->setStatusCode(204); } @@ -118,17 +116,15 @@ public function run($IDs) $extraColumns = $isManyToMany ? $bodyParams : []; // junction table update is expected. inserting 2nd record won't be valid solution. - if (count($to_link) === 0 && count($extraColumns) > 0) + if (count($to_link) === 0 && count($extraColumns) > 0) { throw new ServerErrorHttpException('objects already linked.'); - - else if (count($to_link) === 0) + } else if (count($to_link) === 0) { Yii::$app->getResponse()->setStatusCode(304); - - else { + } else { foreach ($to_link as $model) { - if ($this->checkAccess) + if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id, $model); - + } $relModel->link($this->relationName, $model, $extraColumns); } Yii::$app->getResponse()->setStatusCode(204); diff --git a/src/UnlinkAction.php b/src/UnlinkAction.php index cc8448f..362a86c 100755 --- a/src/UnlinkAction.php +++ b/src/UnlinkAction.php @@ -40,20 +40,22 @@ public function run($IDs) $to_unlink = []; foreach ($ids as $pk_value) { $linked = $relModel->$getter()->andWhere([$pk => $pk_value])->exists(); - if ($linked === true) $to_unlink []= $this->findModel($pk_value); - else throw new BadRequestHttpException(StringHelper::basename($modelClass) . " '$pk_value' not linked to ".StringHelper::basename($this->relativeClass)." '$this->relative_id'."); + if ($linked === true) { + $to_unlink[] = $this->findModel($pk_value); + } else { + throw new BadRequestHttpException(StringHelper::basename($modelClass) . " '$pk_value' not linked to ".StringHelper::basename($this->relativeClass)." '$this->relative_id'."); + } } $relType = $relModel->getRelation($this->relationName); $delete = ($relType->multiple === true && $relType->via !== null); foreach ($to_unlink as $model) { - if ($this->checkAccess) + if ($this->checkAccess) { call_user_func($this->checkAccess, $this->id, $model); - + } $relModel->unlink($this->relationName, $model, $delete); } - Yii::$app->getResponse()->setStatusCode(204); } } diff --git a/src/UnlinkAllAction.php b/src/UnlinkAllAction.php index 69532be..8a30a0d 100755 --- a/src/UnlinkAllAction.php +++ b/src/UnlinkAllAction.php @@ -16,7 +16,7 @@ */ class UnlinkAllAction extends Action { - /** + /** * Unlinks all the related models. * If the relation type is a many_to_many. related row in the junction table will be deleted. * Otherwise related foreign key will be simply set to NULL. @@ -26,7 +26,7 @@ class UnlinkAllAction extends Action public function run() { $relModel = $this->getRelativeModel(); - + $relType = $relModel->getRelation($this->relationName); $delete = ($relType->multiple === true && $relType->via !== null); diff --git a/src/UrlRule.php b/src/UrlRule.php index 28a62c8..9f9c8d6 100644 --- a/src/UrlRule.php +++ b/src/UrlRule.php @@ -131,23 +131,31 @@ protected function setRulesFactory($config) public function init() { parent::init(); - if (empty($this->modelClass)) + if (empty($this->modelClass)) { throw new InvalidConfigException('"modelClass" must be set.'); + } - if (empty($this->relations)) + if (empty($this->relations)) { throw new InvalidConfigException('"relations" must be set.'); + } $this->config['patterns'] = $this->patterns; $this->config['tokens'] = $this->tokens; - if (!empty($this->only)) $this->config['only'] = $this->only; - if (!empty($this->except)) $this->config['except'] = $this->except; - if (!empty($this->extraPatterns)) $this->config['extraPatterns'] = $this->extraPatterns; + if (!empty($this->only)) { + $this->config['only'] = $this->only; + } + if (!empty($this->except)) { + $this->config['except'] = $this->except; + } + if (!empty($this->extraPatterns)) { + $this->config['extraPatterns'] = $this->extraPatterns; + } } /** * @inheritdoc */ - public function createUrl($manager, $route, $params) + public function createUrl($manager, $route, $params) { if ($this->rulesFactory) { unset($params['relativeClass'], $params['relationName'], $params['linkAttribute']); @@ -163,34 +171,35 @@ public function parseRequest($manager, $request) { $modelName = Inflector::camel2id(StringHelper::basename($this->modelClass)); - if ( isset($this->resourceName) ) { + if (isset($this->resourceName)) { $resourceName = $this->resourceName; - } - else { + } else { $resourceName = $this->pluralize ? Inflector::pluralize($modelName) : $modelName; } $link_attribute = isset($this->linkAttribute) ? $this->linkAttribute : $modelName . '_id'; - $this->config['prefix'] = $resourceName . '/<' .$link_attribute. ':\d+>'; + $this->config['prefix'] = $resourceName . '/<' . $link_attribute . ':\d+>'; foreach ($this->relations as $key => $value) { if (is_int($key)) { $relation = $value; $urlName = $this->pluralize ? Inflector::camel2id(Inflector::pluralize($relation)) : Inflector::camel2id($relation); $controller = Inflector::camel2id(Inflector::singularize($relation)); - } - else { + } else { $relation = $key; - if (is_array($value)) list($urlName, $controller) = each($value); - else { + if (is_array($value)) { + list($urlName, $controller) = each($value); + } else { $urlName = $this->pluralize ? Inflector::camel2id(Inflector::pluralize($relation)) : Inflector::camel2id($relation); $controller = $value; } } - if (YII_DEBUG) (new $this->modelClass)->getRelation($relation); + if (YII_DEBUG) { + (new $this->modelClass)->getRelation($relation); + } - $modulePrefix = isset($this->modulePrefix) ? $this->modulePrefix .'/' : ''; + $modulePrefix = isset($this->modulePrefix) ? $this->modulePrefix . '/' : ''; $this->config['controller'][$urlName] = $modulePrefix . $controller; $this->setRulesFactory($this->config); @@ -206,4 +215,4 @@ public function parseRequest($manager, $request) return false; } -} \ No newline at end of file +}