Completed
Push — 2.0 ( ada449...a2425b )
by Vermeulen
01:55
created

Basic::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace BfwSql\Observers;
4
5
use \Exception;
6
use \BfwSql\Actions\AbstractActions;
7
8
/**
9
 * Requests observer.
10
 * Create a log with requests executed and informations about it.
11
 * 
12
 * @package bfw-sql
13
 * @author Vermeulen Maxime <[email protected]>
14
 * @version 2.0
15
 */
16
class Basic implements \SplObserver
17
{
18
    /**
19
     * @const ERR_SYSTEM_QUERY_CONTEXT_CLASS Exception code if we receive the
20
     * event "system query" with(out) a query generated by the system.
21
     */
22
    const ERR_SYSTEM_QUERY_CONTEXT_CLASS = 2401001;
23
    
24
    /**
25
     * @var \BFW\Monolog $monolog The bfw monolog instance define for bfw-sql
26
     */
27
    protected $monolog;
28
    
29
    /**
30
     * @var string $action The last action to send to observers
31
     */
32
    protected $action = '';
33
    
34
    /**
35
     * @var mixed $context The context to send to observers
36
     */
37
    protected $context = null;
38
    
39
    /**
40
     * Constructor
41
     * 
42
     * @param \BFW\Monolog $monolog
43
     */
44
    public function __construct(\BFW\Monolog $monolog)
45
    {
46
        $this->monolog = $monolog;
47
    }
48
    
49
    /**
50
     * Getter accessor to monolog property
51
     * 
52
     * @return \BFW\Monolog
53
     */
54
    public function getMonolog()
55
    {
56
        return $this->monolog;
57
    }
58
    
59
    /**
60
     * Getter accessor to action property
61
     * 
62
     * @return string
63
     */
64
    public function getAction()
65
    {
66
        return $this->action;
67
    }
68
    
69
    /**
70
     * Getter accessor to context property
71
     * 
72
     * @return mixed
73
     */
74
    public function getContext()
75
    {
76
        return $this->context;
77
    }
78
    
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function update(\SplSubject $subject)
83
    {
84
        if ($this->haveMonologHandler() === false) {
85
            return;
86
        }
87
        
88
        $this->action  = $subject->getAction();
89
        $this->context = $subject->getContext();
90
        
91
        $this->analyzeUpdate();
92
    }
93
    
94
    /**
95
     * Check if there are handlers declared to monolog.
96
     * Not need to run the system if there are no place to send the infos.
97
     * 
98
     * @return boolean
99
     */
100
    protected function haveMonologHandler()
101
    {
102
        $handlers = $this->monolog->getHandlers();
103
        
104
        return !empty($handlers);
105
    }
106
    
107
    /**
108
     * Analyze the update sent by subjects to search if the notify is for us
109
     * 
110
     * @return void
111
     */
112
    protected function analyzeUpdate()
113
    {
114
        if ($this->action === 'user query') {
115
            $this->userQuery();
116
        } elseif ($this->action === 'system query') {
117
            $this->systemQuery();
118
        }
119
    }
120
    
121
    /**
122
     * Log a query generate/writed by an user.
123
     * 
124
     * @return void
125
     */
126
    protected function userQuery()
127
    {
128
        $query = $this->context->request;
129
        $error = $this->context->error;
130
        
131
        $this->addQueryToMonoLog($query, $error);
132
    }
133
    
134
    /**
135
     * Log a query generated by AbstractAction system.
136
     * 
137
     * @throws Exception If the context not contain an AbstractAction object.
138
     * 
139
     * @return void
140
     */
141
    protected function systemQuery()
142
    {
143 View Code Duplication
        if ($this->context instanceof AbstractActions === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
            throw new Exception(
145
                '"system query" event should have an AbstractActions class'
146
                .' into the context.',
147
                self::ERR_SYSTEM_QUERY_CONTEXT_CLASS
148
            );
149
        }
150
        
151
        $query = $this->context->getAssembledRequest();
152
        $error = $this->context->getLastErrorInfos();
153
        
154
        $this->addQueryToMonoLog($query, $error);
155
    }
156
    
157
    /**
158
     * Send a query to monolog
159
     * 
160
     * @param string $query The query to log
161
     * @param array $error The error infos about the query
162
     * 
163
     * @return void
164
     */
165
    protected function addQueryToMonoLog($query, $error)
166
    {
167
        $this->monolog->getLogger()->debug(
168
            'Type: '.$this->action.' ; '
169
            .'Query: '.$query. ' ; '
170
            .'Errors: '.print_r($error, true)
171
        );
172
    }
173
}
174