Completed
Pull Request — master (#28)
by
unknown
01:11
created

Xhgui_Saver_Pdo::save()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 7.0496
c 0
b 0
f 0
cc 10
nc 10
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PDO handler to save profiler data
4
 */
5
class Xhgui_Saver_Pdo implements \Xhgui_Saver_Interface {
6
7
    /**
8
     * @var \PDO
9
     */
10
    protected $connection;
11
12
    /**
13
     * PDO constructor.
14
     *
15
     * @param string $dsn
16
     * @param string $userName
17
     * @param string $password
18
     * @param array  $options
19
     */
20
    public function __construct($dsn, $userName, $password, $options = array())
21
    {
22
        $this->connection = new \PDO($dsn, $userName, $password, $options);
23
        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
24
    }
25
26
    /**
27
     * Save data in sql database.
28
     *
29
     * @param array $data
30
     *
31
     * @return bool
32
     */
33
    public function save(array $data)
34
    {
35
        $this->connection->beginTransaction();
36
37
        try {
38
            $id = Xhgui_Util::getId($data);
39
40
            $requestTime = \DateTime::createFromFormat('U u', $data['meta']['request_ts_micro']['sec'].' '.$data['meta']['request_ts_micro']['usec']);
41
42
            $infoStatement = $this->connection->prepare('
43
insert into profiles_info(
44
    id, url, simple_url, request_time, method, main_ct, main_wt, main_cpu, main_mu, main_pmu, application, version, branch,controller, action, remote_addr, session_id 
45
) values (
46
    :id, :url, :simple_url, :request_time, :method, :main_ct, :main_wt, :main_cpu, :main_mu, :main_pmu, :application, :version, :branch, :controller, :action, :remote_addr, :session_id          
47
)');
48
49
            // get some data from meta and save in separate column to make it easier to search/filter
50
            $infoStatement->execute(array(
51
                'id' => $id,
52
                'url'=> $data['meta']['url'],
53
                'simple_url' => $data['meta']['simple_url'],
54
                'request_time' => $requestTime->format('Y-m-d H:i:s.u'),
55
                'main_ct'=> $data['profile']['main()']['ct'],
56
                'main_wt'=> $data['profile']['main()']['wt'],
57
                'main_cpu' => $data['profile']['main()']['cpu'],
58
                'main_mu'=> $data['profile']['main()']['mu'],
59
                'main_pmu' => $data['profile']['main()']['pmu'],
60
                'application'=> !empty($data['meta']['application']) ? $data['meta']['application'] : null,
61
                'version'=> !empty($data['meta']['version']) ? $data['meta']['version'] : null,
62
                'branch' => !empty($data['meta']['branch']) ? $data['meta']['branch'] : null,
63
                'controller' => !empty($data['meta']['controller']) ? $data['meta']['controller'] : null,
64
                'action' => !empty($data['meta']['action']) ? $data['meta']['action'] : null,
65
                'session_id' => !empty($data['meta']['session_id']) ? $data['meta']['action'] : null,
66
                'method' => !empty($data['meta']['method']) ? $data['meta']['method'] : Xhgui_Util::getMethod(),
67
                'remote_addr' => !empty($data['meta']['SERVER']['REMOTE_ADDR']) ? $data['meta']['SERVER']['REMOTE_ADDR'] : null,
68
            ));
69
70
            $profileStatement = $this->connection->prepare('insert into profiles(profile_id, profiles) VALUES(:id, :profiles)');
71
            $profileStatement->execute(array(
72
                'id' => $id,
73
                'profiles' => json_encode($data['profile']),
74
            ));
75
76
            $metaStatement = $this->connection->prepare('insert into profiles_meta(profile_id, meta) VALUES(:id, :meta)');
77
            $metaStatement->execute(array(
78
                'id' => $id,
79
                'meta' => json_encode($data['meta']),
80
            ));
81
82
            $this->connection->commit();
83
84
            return true;
85
        } catch (\Exception $e) {
86
            $this->connection->rollBack();
87
            error_log('xhgui - ' . $e->getMessage());
88
        }
89
        return false;
90
    }
91
}
92