Passed
Push — master ( ed0a8b...c8431f )
by Aimeos
05:25
created

Site::siteCondition()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
cc 8
nc 12
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022
6
 * @package MShop
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\MShop\Common\Manager;
12
13
use \Aimeos\MShop\Locale\Manager\Base as Locale;
14
15
16
/**
17
 * Site trait for managers
18
 *
19
 * @package MShop
20
 * @subpackage Common
21
 */
22
trait Site
23
{
24
	private static $siteInactive = [];
25
26
27
	/**
28
	 * Returns the context object.
29
	 *
30
	 * @return \Aimeos\MShop\ContextIface Context object
31
	 */
32
	abstract protected function context() : \Aimeos\MShop\ContextIface;
33
34
35
	/**
36
	 * Returns a filter object.
37
	 *
38
	 * @return \Aimeos\Base\Criteria\Iface Filter object
39
	 */
40
	abstract public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface;
41
42
43
	/**
44
	 * Returns the site expression for the given name
45
	 *
46
	 * @param string $name Name of the site condition
47
	 * @param int $sitelevel Site level constant from \Aimeos\MShop\Locale\Manager\Base
48
	 * @return \Aimeos\Base\Criteria\Expression\Iface Site search condition
49
	 * @since 2022.04
50
	 */
51
	protected function siteCondition( string $name, int $sitelevel ) : \Aimeos\Base\Criteria\Expression\Iface
52
	{
53
		$sites = $this->context()->locale()->getSites();
54
		$current = $sites[Locale::SITE_ONE] ?? null;
55
		$values = [''];
56
57
		if( isset( $sites[Locale::SITE_PATH] ) && $sitelevel & Locale::SITE_PATH ) {
58
			$values = array_merge( $values, $sites[Locale::SITE_PATH] );
59
		} elseif( $current ) {
60
			$values[] = $current;
61
		}
62
63
		$filter = $this->filter();
64
		$cond = [$filter->compare( '==', $name, $values )];
65
66
		if( isset( $sites[Locale::SITE_SUBTREE] ) && $sitelevel & Locale::SITE_SUBTREE ) {
67
			$cond[] = $filter->compare( '=~', $name, $sites[Locale::SITE_SUBTREE] );
68
		}
69
70
		if( $current && !( $inactive = $this->siteInactive( $current ) )->isEmpty() )
71
		{
72
			return $filter->and( [
73
				$filter->is( $name, '!=', $inactive ),
74
				$filter->or( $cond )
75
			] );
76
		}
77
78
		return $filter->or( $cond );
79
	}
80
81
82
	/**
83
	 * Returns the site IDs that are inactive
84
	 *
85
	 * @param string $current Current site ID
86
	 * @return \Aimeos\Map List of inactive site IDs
87
	 */
88
	protected function siteInactive( string $current ) : \Aimeos\Map
89
	{
90
		// Required for fetching customer item below
91
		if( !strncmp( current( $this->getResourceType( false ) ), 'customer', 8 ) ) {
0 ignored issues
show
Bug introduced by
It seems like getResourceType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
		if( !strncmp( current( $this->/** @scrutinizer ignore-call */ getResourceType( false ) ), 'customer', 8 ) ) {
Loading history...
92
			return map();
93
		}
94
95
		if( !isset( self::$siteInactive[$current] ) )
96
		{
97
			$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
98
			$search = $manager->filter()->add( 'locale.site.siteid', '=~', $current )->add( 'locale.site.status', '<', 1 );
99
			$sites = $manager->search( $search )->getSiteId();
100
101
			if( ( $userId = $this->context()->user() ) )
102
			{
103
				$siteId = \Aimeos\MShop::create( $this->context(), 'customer' )->get( $userId )->getSiteId();
104
105
				$sites = $sites->filter( function( $item ) use ( $siteId ) {
106
					return strncmp( $item, $siteId, strlen( $siteId ) );
107
				} );
108
			}
109
110
			self::$siteInactive[$current] = $sites;
111
		}
112
113
		return self::$siteInactive[$current];
114
	}
115
116
117
	/**
118
	 * Returns the site ID that should be use based on the site level
119
	 *
120
	 * @param string $siteId Site ID to check
121
	 * @param int $sitelevel Site level to check against
122
	 * @return string Site ID that should be use based on the site level
123
	 * @since 2022.04
124
	 */
125
	protected function siteId( string $siteId, int $sitelevel ) : string
126
	{
127
		$sites = $this->context()->locale()->getSites();
128
129
		if( ( $sitelevel & Locale::SITE_ONE ) && isset( $sites[Locale::SITE_ONE] )
130
			&& $siteId === $sites[Locale::SITE_ONE]
131
		) {
132
			return $siteId;
133
		}
134
135
		if( ( $sitelevel & Locale::SITE_PATH ) && isset( $sites[Locale::SITE_PATH] )
136
			&& in_array( $siteId, $sites[Locale::SITE_PATH] )
137
		) {
138
			return $siteId;
139
		}
140
141
		if( ( $sitelevel & Locale::SITE_SUBTREE ) && isset( $sites[Locale::SITE_SUBTREE] )
142
			&& !strncmp( $sites[Locale::SITE_SUBTREE], $siteId, strlen( $sites[Locale::SITE_SUBTREE] ) )
143
		) {
144
			return $siteId;
145
		}
146
147
		return $this->context()->locale()->getSiteId();
148
	}
149
150
151
	/**
152
	 * Returns the site expression for the given name
153
	 *
154
	 * @param string $name SQL name for the site condition
155
	 * @param int $sitelevel Site level constant from \Aimeos\MShop\Locale\Manager\Base
156
	 * @return string Site search condition
157
	 * @since 2022.04
158
	 */
159
	protected function siteString( string $name, int $sitelevel ) : string
160
	{
161
		$translation = ['marker' => $name];
162
		$types = ['marker' => \Aimeos\Base\DB\Statement\Base::PARAM_STR];
163
164
		return $this->siteCondition( 'marker', $sitelevel )->toSource( $types, $translation );
165
	}
166
}
167