Failed Conditions
Pull Request — experimental/sf (#31)
by Kentaro
06:59
created

PageRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 216
Duplicated Lines 10.19 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
dl 22
loc 216
rs 10
c 0
b 0
f 0
ccs 96
cts 99
cp 0.9697
wmc 15
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A findUnusedBlocks() 0 21 3
A getByDeviceTypeAndId() 0 45 4
A getByUrl() 0 19 1
A newPage() 0 9 1
A findOrCreate() 0 11 2
A getPageList() 22 22 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Repository;
15
16
use Doctrine\ORM\NoResultException;
17
use Eccube\Common\EccubeConfig;
18
use Eccube\Entity\Master\DeviceType;
19
use Eccube\Entity\Page;
20
use Symfony\Bridge\Doctrine\RegistryInterface;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
/**
24
 * PageRepository
25
 *
26
 * This class was generated by the Doctrine ORM. Add your own custom
27
 * repository methods below.
28
 */
29
class PageRepository extends AbstractRepository
30
{
31
    /**
32
     * @var EccubeConfig
33
     */
34
    protected $eccubeConfig;
35
36
    /**
37
     * @var string
38
     * @path %eccube_theme_user_data_dir% (app/template/user_data)
39
     */
40
    protected $userDataRealDir;
41
42
    /**
43
     * @var string
44
     * @path %eccube_theme_app_dir% (app/template)
45
     */
46
    protected $templateRealDir;
47
48
    /**
49
     * @var string
50
     * @path %eccube_theme_src_dir% (src/Eccube/Resource/template)
51
     */
52
    protected $templateDefaultRealDir;
53
54
    /**
55
     * PageRepository constructor.
56
     *
57
     * @param RegistryInterface $registry
58
     * @param EccubeConfig $eccubeConfig
59
     * @param ContainerInterface $container
60 712
     */
61
    public function __construct(RegistryInterface $registry, EccubeConfig $eccubeConfig, ContainerInterface $container)
62 712
    {
63 712
        parent::__construct($registry, Page::class);
64 712
        $this->eccubeConfig = $eccubeConfig;
65 712
        $this->userDataRealDir = $container->getParameter('eccube_theme_user_data_dir');
66 712
        $this->templateRealDir = $container->getParameter('eccube_theme_app_dir');
67
        $this->templateDefaultRealDir = $container->getParameter('eccube_theme_src_dir');
68
    }
69
70
    /**
71
     * @param DeviceType $DeviceType
72
     * @param int $pageId
73
     *
74
     * @return array
75 1
     */
76
    public function findUnusedBlocks(DeviceType $DeviceType, $pageId)
77 1
    {
78 1
        $em = $this->getEntityManager();
79 1
        $blockRepo = $em->getRepository('Eccube\Entity\Block');
80 1
        $ownBlockPositions = $this->getByDeviceTypeAndId($DeviceType, $pageId)->getBlockPositions();
81 1
        $ids = [];
82 1
        foreach ($ownBlockPositions as $ownBlockPosition) {
83
            $ids[] = $ownBlockPosition->getBlock()->getId();
84
        }
85
86 1
        // $idsが空配列だと、$ids以外のblockを取得するSQLが生成されないため、存在しないidを入れる
87
        if (empty($ids)) {
88
            $ids[] = \Eccube\Entity\Block::UNUSED_BLOCK_ID;
89
        }
90 1
91 1
        return $blockRepo->createQueryBuilder('b')
92 1
            ->where('b.id not in (:ids)')
93 1
            ->setParameter(':ids', $ids)
94 1
            ->getQuery()
95
            ->getResult();
96
    }
97
98
    /**
99
     * @param DeviceType $DeviceType
100
     * @param int $pageId
101
     *
102
     * @return mixed
103 5
     */
104
    public function getByDeviceTypeAndId(DeviceType $DeviceType, $pageId)
105 5
    {
106 5
        $qb = $this->createQueryBuilder('p')
107 5
            ->select('p, bp, b')
108 5
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
109 5
            ->leftJoin('bp.Block', 'b')
110 5
            ->andWhere('p.DeviceType = :DeviceType AND p.id = :pageId')
111 5
            ->addOrderBy('bp.section', 'ASC')
112
            ->addOrderBy('bp.block_row', 'ASC');
113
114 5
        $ownResult = $qb
115 5
            ->getQuery()
116 5
            ->setParameters([
117 5
                'DeviceType' => $DeviceType,
118
                'pageId' => $pageId,
119 5
            ])
120
            ->getSingleResult();
121 5
122 5
        $qb = $this->createQueryBuilder('p')
123 5
            ->select('p, bp, b')
124 5
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
125 5
            ->leftJoin('bp.Block', 'b')
126 5
            ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1')
127 5
            ->addOrderBy('bp.section', 'ASC')
128
            ->addOrderBy('bp.block_row', 'ASC');
129
130 5
        $anyResults = $qb
131 5
            ->getQuery()
132 5
            ->setParameters([
133
                'DeviceType' => $DeviceType,
134 5
            ])
135
            ->getResult();
136 5
137 5
        $OwnBlockPosition = $ownResult->getBlockPositions();
138 5
        foreach ($anyResults as $anyResult) {
139 5
            $BlockPositions = $anyResult->getBlockPositions();
140 5
            foreach ($BlockPositions as $BlockPosition) {
141 5
                if (!$OwnBlockPosition->contains($BlockPosition)) {
142
                    $ownResult->addBlockPosition($BlockPosition);
143
                }
144
            }
145
        }
146 5
147
        return $ownResult;
148
    }
149
150
    /**
151
     * @param DeviceType $DeviceType
152
     * @param string $url
153
     * @throw NoResultException
154
     *
155 1
     * @return Page
156
     */
157
    public function getByUrl(DeviceType $DeviceType, $url)
158
    {
159
        $qb = $this->createQueryBuilder('p');
160 1
        $Page = $qb->select('p, pll,l, bp, b')
161
            ->leftJoin('p.PageLayouts', 'pll')
162 1
            ->leftJoin('pll.Layout', 'l')
163 1
            ->leftJoin('l.BlockPositions', 'bp')
164 1
            ->leftJoin('bp.Block', 'b')
165 1
            ->where('p.url = :route')
166 1
            ->andWhere('l.DeviceType = :DeviceType')
167 1
            ->orderBy('bp.block_row', 'ASC')
168 1
            ->setParameter('route', $url)
169
            ->setParameter('DeviceType', $DeviceType)
170
            ->getQuery()
171 1
            ->useResultCache(true, $this->getCacheLifetime())
172 1
            ->getSingleResult();
173 1
174 1
        return $Page;
175 1
    }
176
177 1
    /**
178
     * @param DeviceType $DeviceType
179 1
     *
180
     * @return Page
181
     */
182
    public function newPage(DeviceType $DeviceType)
183 1
    {
184 1
        $Page = new \Eccube\Entity\Page();
185 1
        $Page
186 1
            ->setDeviceType($DeviceType)
187 1
            ->setEditType(Page::EDIT_TYPE_USER);
188 1
189 1
        return $Page;
190
    }
191
192 1
    /**
193 1
     * @param int $page_id
194 1
     * @param DeviceType $DeviceType
195 1
     *
196
     * @return Page|mixed
197 1
     */
198
    public function findOrCreate($page_id, DeviceType $DeviceType)
199 1
    {
200 1
        if (is_null($page_id)) {
201 1
            $Page = $this
202 1
                ->newPage($DeviceType);
203
204
            return $Page;
205 1
        } else {
206 1
            return $this->getByDeviceTypeAndId($DeviceType, $page_id);
207 1
        }
208 1
    }
209
210 1
    /**
211
     * ページの属性を取得する.
212
     *
213
     * この関数は, dtb_Page の情報を検索する.
214
     * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC.
215 1
     *
216
     * @param  \Eccube\Entity\Master\DeviceType  $DeviceType 端末種別ID
217
     * @param  string                            $where 追加の検索条件
218
     * @param  string[]                          $parameters 追加の検索パラメーター
219
     *
220
     * @return array                             ページ属性の配列
221
     */
222 View Code Duplication
    public function getPageList(DeviceType $DeviceType, $where = null, $parameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
223 126
    {
224
        $qb = $this->createQueryBuilder('l')
225 126
            ->orderBy('l.id', 'DESC')
226
            ->where('l.DeviceType = :DeviceType')
227 126
            ->setParameter('DeviceType', $DeviceType)
228 126
            ->andWhere('l.id <> 0')
229
            ->andWhere('l.MasterPage is null')
230 126
            ->orderBy('l.id', 'ASC');
231
        if (!is_null($where)) {
232
            $qb->andWhere($where);
233
            foreach ($parameters as $key => $val) {
234
                $qb->setParameter($key, $val);
235
            }
236
        }
237
238
        $Pages = $qb
239 5
            ->getQuery()
240
            ->getResult();
241 5
242
        return $Pages;
243 2
    }
244
}
245