Completed
Push — experimental/sf ( 088e42...fc9b4d )
by Kiyotaka
363:35 queued 321:24
created

PageRepository::getPageList()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 3
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
ccs 12
cts 15
cp 0.8
crap 3.072
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
     */
61 774
    public function __construct(RegistryInterface $registry, EccubeConfig $eccubeConfig, ContainerInterface $container)
62
    {
63 774
        parent::__construct($registry, Page::class);
64 774
        $this->eccubeConfig = $eccubeConfig;
65 774
        $this->userDataRealDir = $container->getParameter('eccube_theme_user_data_dir');
66 774
        $this->templateRealDir = $container->getParameter('eccube_theme_app_dir');
67 774
        $this->templateDefaultRealDir = $container->getParameter('eccube_theme_src_dir');
68
    }
69
70
    /**
71
     * @param DeviceType $DeviceType
72
     * @param int $pageId
73
     *
74
     * @return array
75
     */
76 1
    public function findUnusedBlocks(DeviceType $DeviceType, $pageId)
77
    {
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 1
            $ids[] = $ownBlockPosition->getBlock()->getId();
84
        }
85
86
        // $idsが空配列だと、$ids以外のblockを取得するSQLが生成されないため、存在しないidを入れる
87 1
        if (empty($ids)) {
88
            $ids[] = \Eccube\Entity\Block::UNUSED_BLOCK_ID;
89
        }
90
91 1
        return $blockRepo->createQueryBuilder('b')
92 1
            ->where('b.id not in (:ids)')
93 1
            ->setParameter(':ids', $ids)
94 1
            ->getQuery()
95 1
            ->getResult();
96
    }
97
98
    /**
99
     * @param DeviceType $DeviceType
100
     * @param int $pageId
101
     *
102
     * @return mixed
103
     */
104 5
    public function getByDeviceTypeAndId(DeviceType $DeviceType, $pageId)
105
    {
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 5
            ->addOrderBy('bp.block_row', 'ASC');
113
114
        $ownResult = $qb
115 5
            ->getQuery()
116 5
            ->setParameters([
117 5
                'DeviceType' => $DeviceType,
118 5
                'pageId' => $pageId,
119
            ])
120 5
            ->getSingleResult();
121
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 5
            ->addOrderBy('bp.block_row', 'ASC');
129
130
        $anyResults = $qb
131 5
            ->getQuery()
132 5
            ->setParameters([
133 5
                'DeviceType' => $DeviceType,
134
            ])
135 5
            ->getResult();
136
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 5
                    $ownResult->addBlockPosition($BlockPosition);
143
                }
144
            }
145
        }
146
147 5
        return $ownResult;
148
    }
149
150
    /**
151
     * @param DeviceType $DeviceType
152
     * @param string $url
153
     * @throw NoResultException
154
     *
155
     * @return Page
156
     */
157 159
    public function getByUrl(DeviceType $DeviceType, $url)
158
    {
159 159
        $qb = $this->createQueryBuilder('p');
160 159
        $Page = $qb->select('p, pll,l, bp, b')
161 159
            ->leftJoin('p.PageLayouts', 'pll')
162 159
            ->leftJoin('pll.Layout', 'l')
163 159
            ->leftJoin('l.BlockPositions', 'bp')
164 159
            ->leftJoin('bp.Block', 'b')
165 159
            ->where('p.url = :route')
166 159
            ->andWhere('l.DeviceType = :DeviceType')
167 159
            ->orderBy('bp.block_row', 'ASC')
168 159
            ->setParameter('route', $url)
169 159
            ->setParameter('DeviceType', $DeviceType)
170 159
            ->getQuery()
171 159
            ->useResultCache(true, $this->getCacheLifetime())
172 159
            ->getSingleResult();
173
174 144
        return $Page;
175
    }
176
177
    /**
178
     * @param DeviceType $DeviceType
179
     *
180
     * @return Page
181
     */
182 125
    public function newPage(DeviceType $DeviceType)
183
    {
184 125
        $Page = new \Eccube\Entity\Page();
185
        $Page
186 125
            ->setDeviceType($DeviceType)
187 125
            ->setEditType(Page::EDIT_TYPE_USER);
188
189 125
        return $Page;
190
    }
191
192
    /**
193
     * @param int $page_id
194
     * @param DeviceType $DeviceType
195
     *
196
     * @return Page|mixed
197
     */
198 5
    public function findOrCreate($page_id, DeviceType $DeviceType)
199
    {
200 5
        if (is_null($page_id)) {
201
            $Page = $this
202 2
                ->newPage($DeviceType);
203
204 2
            return $Page;
205
        } else {
206 3
            return $this->getByDeviceTypeAndId($DeviceType, $page_id);
207
        }
208
    }
209
210
    /**
211
     * ページの属性を取得する.
212
     *
213
     * この関数は, dtb_Page の情報を検索する.
214
     * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC.
215
     *
216
     * @param  \Eccube\Entity\Master\DeviceType  $DeviceType 端末種別ID
217
     * @param  string                            $where 追加の検索条件
218
     * @param  string[]                          $parameters 追加の検索パラメーター
219
     *
220
     * @return array                             ページ属性の配列
221
     */
222 2 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
    {
224 2
        $qb = $this->createQueryBuilder('l')
225 2
            ->orderBy('l.id', 'DESC')
226 2
            ->where('l.DeviceType = :DeviceType')
227 2
            ->setParameter('DeviceType', $DeviceType)
228 2
            ->andWhere('l.id <> 0')
229 2
            ->andWhere('l.MasterPage is null')
230 2
            ->orderBy('l.id', 'ASC');
231 2
        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 2
            ->getQuery()
240 2
            ->getResult();
241
242 2
        return $Pages;
243
    }
244
}
245