-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
SiteRepository.php
114 lines (104 loc) · 2.84 KB
/
SiteRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Neos\Neos\Domain\Repository;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\QueryInterface;
use Neos\Flow\Persistence\QueryResultInterface;
use Neos\Flow\Persistence\Repository;
use Neos\Neos\Domain\Model\Site;
use Neos\Neos\Domain\Exception as NeosException;
/**
* The Site Repository
*
* @Flow\Scope("singleton")
* @api
*/
class SiteRepository extends Repository
{
/**
* @var array
*/
protected $defaultOrderings = [
'name' => QueryInterface::ORDER_ASCENDING,
'nodeName' => QueryInterface::ORDER_ASCENDING
];
/**
* @Flow\InjectConfiguration(package="Neos.Neos", path="defaultSiteNodeName")
* @var string
*/
protected $defaultSiteNodeName;
/**
* Finds the first site
*
* @return Site The first site or NULL if none exists
* @api
*/
public function findFirst()
{
return $this->createQuery()->execute()->getFirst();
}
/**
* Find all sites with status "online"
*
* @return QueryResultInterface
*/
public function findOnline()
{
return $this->findByState(Site::STATE_ONLINE);
}
/**
* Find first site with status "online"
*
* @return ?Site
*/
public function findFirstOnline()
{
return $this->findOnline()->getFirst();
}
/**
* @param string $nodeName
* @return Site|null
*/
public function findOneByNodeName(string $nodeName): ?Site
{
$query = $this->createQuery();
/** @var Site|null $site */
$site = $query->matching(
$query->equals('nodeName', $nodeName)
)
->execute()
->getFirst();
return $site;
}
/**
* Find the site that was specified in the configuration ``defaultSiteNodeName``
*
* If the defaultSiteNodeName-setting is null the first active site is returned
* If the site is not found or not active an exception is thrown
*
* @return Site
* @throws NeosException
*/
public function findDefault()
{
if ($this->defaultSiteNodeName === null) {
return $this->findOnline()->getFirst();
}
/**
* @var Site $defaultSite
*/
$defaultSite = $this->findOneByNodeName($this->defaultSiteNodeName);
if (!$defaultSite instanceof Site || $defaultSite->getState() !== Site::STATE_ONLINE) {
throw new NeosException(sprintf('DefaultSiteNode %s not found or not active', $this->defaultSiteNodeName), 1476374818);
}
return $defaultSite;
}
}