Completed
Push — master ( ce185a...200fd3 )
by David
01:55
created

TagManagerSmarty::toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Tag Manager
4
 * Copyright (c) Webmatch GmbH
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 */
16
17
namespace WbmTagManager\Services;
18
19
use Doctrine\DBAL\Connection;
20
21
class TagManagerSmarty implements TagManagerSmartyInterface
22
{
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
28
    /**
29
     * @var \Enlight_Controller_Front
30
     */
31
    private $front;
32
33
    /**
34
     * @var array
35
     */
36
    private $_cache = [];
37
38
    /**
39
     * @param Connection                $connection
40
     * @param \Enlight_Controller_Front $front
41
     */
42
    public function __construct(
43
        Connection $connection,
44
        \Enlight_Controller_Front $front
45
    ) {
46
        $this->connection = $connection;
47
        $this->front = $front;
48
    }
49
50
    /**
51
     * @param $arguments
52
     *
53
     * @return string
54
     *
55
     * @throws \Exception
56
     */
57
    public function getDbSelect($arguments)
58
    {
59
        if (
60
            empty($arguments['select']) ||
61
            empty($arguments['from'])
62
        ) {
63
            return "";
64
        }
65
66
        $hash = md5($arguments);
67
68
        if (isset($this->_cache[$hash])) {
69
            return $this->_cache[$hash];
70
        }
71
72
        $qb = $this->connection->createQueryBuilder();
73
74
        $qb->select($arguments['select'])
75
            ->from($arguments['from']);
76
77
        if (is_array($arguments['where'])) {
78
            foreach ($arguments['where'] as $column => $value) {
79
                $qb->andWhere(
80
                    sprintf(
81
                        '%s %s',
82
                        $column,
83
                        $qb->createNamedParameter($value)
84
                    )
85
                );
86
            }
87
        }
88
89
        if (is_array($arguments['order'])) {
90
            foreach ($arguments['order'] as $column => $order) {
91
                $qb->addOrderBy($column, $order);
92
            }
93
        }
94
95
        try {
96
            $value = $qb->execute()->fetch(\PDO::FETCH_COLUMN);
97
            $this->_cache[$hash] = $value;
98
99
            return $value;
100
        } catch (\Exception $exception) {
101
            return "";
102
        }
103
    }
104
105
    /**
106
     * @param $arguments
107
     *
108
     * @return string
109
     */
110
    public function requestGet($arguments)
111
    {
112
        if (
113
        empty($arguments['param'])
114
        ) {
115
            return "";
116
        }
117
118
        return $this->front->Request()->get($arguments['param']);
119
    }
120
121
    /**
122
     * @param mixed $value
123
     *
124
     * @return string
125
     */
126
    public function toString($value)
127
    {
128
        return sprintf('\"%s\"', $value);
129
    }
130
}
131