Completed
Pull Request — master (#6)
by Angel
03:38
created

ProctRecordAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 37
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 9 2
A errorException() 0 3 1
1
<?php
2
3
namespace roaresearch\yii2\roa\actions;
4
5
use roarsearch\yii2\roa\hal\ARContract;
0 ignored issues
show
Bug introduced by
The type roarsearch\yii2\roa\hal\ARContract was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Yii;
7
use yii\web\{HttpException, ServerErrorHttpException};
8
9
/**
10
 * Proct a record. Returns the record on success, throws exception on error.
11
 *
12
 * @author Angel (Faryshta) Guevara <[email protected]>
13
 */
14
abstract class ProctRecordAction extends Action
15
{
16
    /**
17
     * @var string error message thrown when proct fails.
18
     */
19
    protected string $errorMessage = 'Process failed for unknown reasons.';
20
21
    /**
22
     * Procts a record.
23
     *
24
     * @param mixed $id the identifier value.
25
     * @return ARContract the record after successful proct
26
     */
27
    public function run($id): ARContract
28
    {
29
        $this->checkAccess(
30
            ($model = $this->findModel($id)),
31
            ($params = Yii::$app->request->getQueryParams())
32
        );
33
        $this->proct($model, $params) ?: throw $this->errorException();
34
35
        return $model;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model returns the type yii\db\ActiveRecordInterface which is incompatible with the type-hinted return roarsearch\yii2\roa\hal\ARContract.
Loading history...
36
    }
37
38
    /**
39
     * @param ARContract $model
40
     * @param array $params the request params
41
     * @return bool whether the proct was successful
42
     */
43
    abstract protected function proct(ARContract $model, array $params): bool;
44
45
    /**
46
     * @return HttpException
47
     */
48
    protected function errorException(): HttpException
49
    {
50
        return new ServerErrorHttpException($this->errorMessage);
51
    }
52
}
53
54