Completed
Push — master ( 077f63...ceb768 )
by Andrii
02:53
created

BaseCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 3
cbo 2
dl 0
loc 68
ccs 0
cts 48
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getHandler() 0 8 2
A setHandler() 0 4 1
A loadFromRequest() 0 7 2
A getRequestData() 0 7 1
A getEntityClass() 0 4 1
A getRecordClass() 0 4 1
A getRepository() 0 8 2
A findRepository() 0 4 1
1
<?php
2
/**
3
 * HiAPI Yii2 base project for building API
4
 *
5
 * @link      https://github.com/hiqdev/hiapi
6
 * @package   hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiapi\commands;
12
13
use Yii;
14
15
abstract class BaseCommand extends \yii\base\Model
16
{
17
    protected $entityClass;
18
19
    protected static $handler;
20
21
    public function __construct($entityClass, $config = [])
22
    {
23
        $this->entityClass = $entityClass;
24
        parent::__construct($config);
25
    }
26
27
    public static function getHandler()
28
    {
29
        if (!is_object(static::$handler)) {
30
            static::$handler = Yii::createObject(static::$handler);
31
        }
32
33
        return static::$handler;
34
    }
35
36
    public static function setHandler($value)
37
    {
38
        static::$handler = $value;
39
    }
40
41
    public function loadFromRequest($request)
42
    {
43
        $this->load($this->getRequestData($request), '');
44
        $this->validate();
45
46
        return $this->hasErrors() ? $this->getErrors() : null;
47
    }
48
49
    public function getRequestData($request)
50
    {
51
        $get = $request->get();
52
        $post = $request->post();
53
54
        return array_merge($get, $post);
55
    }
56
57
    public function getEntityClass()
58
    {
59
        return $this->entityClass;
60
    }
61
62
    public function getRecordClass()
63
    {
64
        return $this->getRepository()->getRecordClass();
65
    }
66
67
    protected $_repository;
68
69
    public function getRepository()
70
    {
71
        if ($this->_repository === null) {
72
            $this->_repository = $this->findRepository();
73
        }
74
75
        return $this->_repository;
76
    }
77
78
    public function findRepository()
79
    {
80
        return Yii::$app->entityManager->getRepository($this->entityClass);
81
    }
82
}
83