Completed
Push — master ( 4f3de5...9b9e7a )
by Pavel
05:51
created

BaseController::encode()   C

Complexity

Conditions 9
Paths 40

Size

Total Lines 49
Code Lines 29

Duplication

Lines 6
Ratio 12.24 %

Importance

Changes 0
Metric Value
cc 9
eloc 29
nc 40
nop 4
dl 6
loc 49
rs 5.7446
c 0
b 0
f 0
1
<?php
2
namespace App\Controller;
3
4
use App\Observers;
5
use App\Model;
6
use App\Common\JsonException;
7
use App\Requests\IRequest;
8
9
abstract class BaseController
10
{
11
    /**
12
     * @var \Illuminate\Validation\Factory;
13
     */
14
    public $validation;
15
16
    /**
17
     * @var \App\Common\JsonApiEncoder
18
     */
19
    public $encoder;
20
21
    /**
22
     * @var \App\Common\Renderer
23
     */
24
    public $renderer;
25
26
    /**
27
     * @var array
28
     */
29
    public $settings;
30
31
    /**
32
     * @var \Swift_Mailer
33
     */
34
    public $mailer;
35
36
    /**
37
     * @var \App\Common\MailRenderer
38
     */
39
    public $mailRenderer;
40
41
    /**
42
     * BaseController constructor.
43
     *
44
     * @param $container
45
     */
46
    public function __construct($container)
47
    {
48
        $this->renderer     = $container['renderer'];
49
        $this->validation   = $container['validation'];
50
        $this->settings     = $container['settings'];
51
        $this->mailer       = $container['mailer'];
52
        $this->mailRenderer = $container['mailRenderer'];
53
        $this->encoder      = $container['encoder'];
54
55
        $this->registerModelObservers();
56
    }
57
58
    /**
59
     * @param array|null|object $params
60
     * @param string            $entity
61
     * @param IRequest          $request
62
     *
63
     * @return bool
64
     * @throws JsonException
65
     */
66
    public function validationRequest($params, $entity, $request)
67
    {
68
        if (!isset($params['data']['attributes'])) {
69
            throw new JsonException($entity, 400, 'Invalid Attribute', 'Not required attributes - data.');
70
        }
71
72
        $validator = $this->validation->make($params['data']['attributes'], $request->rules());
73
74
        if ($validator->fails()) {
75
            $messages = implode(' ', $validator->messages()->all());
76
            throw new JsonException($entity, 400, 'Invalid Attribute', $messages);
77
        }
78
79
        return true;
80
    }
81
82
    /**
83
     * Register model observers
84
     */
85
    private function registerModelObservers(){
86
        $observers = [
87
            Observers\CreatedByAndUpdatedByObserver::class => [
88
                Model\Right::class,
89
                Model\Role::class,
90
                Model\User::class,
91
            ],
92
93
            Observers\LoggerObserver::class => [
94
                Model\Right::class,
95
                Model\Role::class,
96
                Model\User::class,
97
            ]
98
        ];
99
100
        foreach($observers as $observer => $models){
101
            foreach($models as $model){
102
                call_user_func($model. '::observe', $observer);
103
            }
104
        }
105
    }
106
}
107