PhpDumper::dumpStructureRecursively()   D
last analyzed

Complexity

Conditions 9
Paths 36

Size

Total Lines 40
Code Lines 23

Duplication

Lines 10
Ratio 25 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 10
loc 40
rs 4.9091
cc 9
eloc 23
nc 36
nop 1
1
<?php
2
3
/*
4
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace ItBlaster\TranslationBundle\Translation\Dumper;
20
21
use ItBlaster\TranslationBundle\Model\Translation;
22
use ItBlaster\TranslationBundle\Model\TranslationQuery;
23
use ItBlaster\TranslationBundle\Traits\TranslationTrait;
24
use JMS\TranslationBundle\Model\Message;
25
use JMS\TranslationBundle\Translation\Dumper\ArrayStructureDumper;
26
use JMS\TranslationBundle\Util\Writer;
27
use JMS\TranslationBundle\Model\MessageCatalogue;
28
29
class PhpDumper extends ArrayStructureDumper
30
{
31
    use TranslationTrait;
32
33
    private $writer;
34
    protected $catalogue;
35
    protected $locale;
36
37
    public function __construct()
38
    {
39
        $this->writer = new Writer();
40
    }
41
42
    public function dump(MessageCatalogue $catalogue, $domain = 'messages')
43
    {
44
        $this->catalogue = $catalogue;
45
        $this->locale = $catalogue->getLocale();
46
        return parent::dump($catalogue, $domain);
47
    }
48
49
    protected function dumpStructure(array $structure)
50
    {
51
        $this->writer
52
            ->reset()
53
            ->writeln('<?php')
54
            ->writeln('return \ItBlaster\TranslationBundle\Model\TranslationPeer::getListForLocale("' . $this->locale . '");')
55
            ->indent();
56
        $this->dumpStructureRecursively($structure);
57
58
        $result = $this->writer
59
            ->outdent()
60
            ->getContent();
61
        return $result;
62
    }
63
64
    private function dumpStructureRecursively(array $structure)
65
    {
66
        $locales = $this->getLocales();
67
        $translation_strings = array();
68
        foreach ($locales as $locale) {
69
            $translation_strings[$locale] = array();
70
            $translation_list = TranslationQuery::create()->joinWithI18n($locale)->find();
71
            foreach ($translation_list as $translation_item) {
72
                /** @var Translation $translation_item */
73
                $translation_item->setLocale($locale);
74
75
                if ($translation_item->getTitle() === null) {
76
                    $translation_item->setTitle('')->save();
77
                }
78
                $translation_strings[$locale][$translation_item->getAlias()] = $translation_item;
79
            }
80
        }
81
82
        foreach ($structure as $k => $v) {
83
            /** @var Message $v */
84 View Code Duplication
            if (!isset($translation_strings['en'][$k])) {
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...
85
                $trans_obj = new Translation();
86
                $this->setParamsTransObj($trans_obj, 'en', $k);
87
                $translation_strings['en'][$k] = $trans_obj;
88
            }
89
90
            foreach ($locales as $locale) {
91
                if ($locale != 'en') {
92 View Code Duplication
                    if (!isset($translation_strings[$locale][$k])) {
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...
93
                        /** @var Translation $trans_obj */
94
                        $trans_obj = $translation_strings['en'][$k];
95
                        $this->setParamsTransObj($trans_obj,$locale,$k);
96
                    }
97
                }
98
            }
99
100
            $this->writer->indent();
101
            $this->writer->outdent();
102
        }
103
    }
104
105
    /**
106
     * Проставляет параметры у объекта перевода
107
     *
108
     * @param $trans_obj
109
     * @param $locale
110
     * @param string $title
111
     */
112
    private function setParamsTransObj(&$trans_obj, $locale, $alias, $title='')
113
    {
114
        $trans_obj
115
            ->setAlias($alias)
116
            ->setLocale($locale)
117
            ->setTitle($title)
118
            ->save();
119
    }
120
}