Completed
Push — master ( 185e5e...dc5aa9 )
by Alexander
03:43
created

PublicationHelper::translateFieldsSubQuery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Oleksandr Torosh <[email protected]>
4
 */
5
6
namespace Publication\Model\Helper;
7
8
use Application\Cache\Keys;
9
use Publication\Model\Publication;
10
11
class PublicationHelper extends Publication
12
{
13
14
    public function publicationBySlug($slug, $lang = null, $lifeTime = 60)
15
    {
16
        $lang = ($lang) ? $lang : LANG;
17
18
        $publicationResult = $this->getDi()->get('cacheManager')->load([
19
            Keys::PUBLICATION,
20
            $slug,
21
            $lang
22
23
        ], function() use ($slug, $lang, $lifeTime) {
24
            $columns = [
25
                'p.*',
26
                't_slug' => 't.slug'
27
            ];
28
            $fields = $this->translateFieldsSubQuery($lang);
29
            $columns = array_merge($columns, $fields);
30
31
            $qb = $this->modelsManager->createBuilder()
0 ignored issues
show
Bug introduced by
The property modelsManager does not seem to exist. Did you mean _modelsManager?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
32
                ->columns($columns)
33
                ->addFrom('Publication\Model\Publication', 'p')
34
                ->innerJoin('Publication\Model\Type', 'p.type_id = t.id', 't')
35
                ->where('p.slug = :slug:', ['slug' => $slug]);
36
37
            $result = $qb->getQuery()->execute()->getFirst();
38
            return $result;
39
40
        }, $lifeTime);
41
42
        return $publicationResult;
43
    }
44
45
    public function translateFieldsSubQuery($lang = null)
46
    {
47
        $lang = ($lang) ? $lang : LANG;
48
        
49
        $fields = [];
50
        foreach($this->translateFields as $field) {
51
            $fields[] = "(SELECT tr.value FROM [$this->translateModel] AS tr WHERE tr.foreign_id = p.id AND tr.lang = '$lang' AND tr.key = '$field') AS $field";
52
        }
53
        return $fields;
54
    }
55
56
}