forked from gmoigneu/magento2-platformsh
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Store.php
1254 lines (1107 loc) · 33.9 KB
/
Store.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Store\Model;
use Magento\Catalog\Model\Category;
use Magento\Directory\Model\Currency;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Http\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ScopeInterface as AppScopeInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Url\ScopeInterface as UrlScopeInterface;
use Magento\Framework\Model\AbstractExtensibleModel;
use Magento\Framework\UrlInterface;
use Magento\Store\Api\Data\StoreInterface;
/**
* Store model
*
* @method Store setGroupId($value)
* @method int getSortOrder()
* @method int getStoreId()
* @method Store setSortOrder($value)
* @method Store setIsActive($value)
*
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
*/
class Store extends AbstractExtensibleModel implements
AppScopeInterface,
UrlScopeInterface,
IdentityInterface,
StoreInterface
{
/**
* Entity name
*/
const ENTITY = 'store';
/**
* Custom entry point param
*/
const CUSTOM_ENTRY_POINT_PARAM = 'custom_entry_point';
/**#@+
* Configuration paths
*/
const XML_PATH_STORE_IN_URL = 'web/url/use_store';
const XML_PATH_USE_REWRITES = 'web/seo/use_rewrites';
const XML_PATH_UNSECURE_BASE_URL = 'web/unsecure/base_url';
const XML_PATH_SECURE_BASE_URL = 'web/secure/base_url';
const XML_PATH_SECURE_IN_FRONTEND = 'web/secure/use_in_frontend';
const XML_PATH_SECURE_IN_ADMINHTML = 'web/secure/use_in_adminhtml';
const XML_PATH_SECURE_BASE_LINK_URL = 'web/secure/base_link_url';
const XML_PATH_UNSECURE_BASE_LINK_URL = 'web/unsecure/base_link_url';
const XML_PATH_SECURE_BASE_STATIC_URL = 'web/secure/base_static_url';
const XML_PATH_UNSECURE_BASE_STATIC_URL = 'web/unsecure/base_static_url';
const XML_PATH_SECURE_BASE_MEDIA_URL = 'web/secure/base_media_url';
const XML_PATH_UNSECURE_BASE_MEDIA_URL = 'web/unsecure/base_media_url';
const XML_PATH_PRICE_SCOPE = 'catalog/price/scope';
/**#@-*/
/**#@+
* Price scope constants
*/
const PRICE_SCOPE_GLOBAL = 0;
const PRICE_SCOPE_WEBSITE = 1;
/**#@-*/
const ADMIN_CODE = 'admin';
/**
* Cache tag
*/
const CACHE_TAG = 'store';
/**
* Script name, which returns all the images
*/
const MEDIA_REWRITE_SCRIPT = 'get.php/';
/**
* A placeholder for generating base URL
*/
const BASE_URL_PLACEHOLDER = '{{base_url}}';
/**
* Identifier of default store
* used for loading data of default scope
*/
const DEFAULT_STORE_ID = 0;
/**
* Default store Id (for install)
*/
const DISTRO_STORE_ID = 1;
/**
* @var \Magento\Framework\App\Cache\Type\Config
*/
protected $_configCacheType;
/**
* Cache flag
*
* @var boolean
*/
protected $_cacheTag = true;
/**
* Event prefix for model events
*
* @var string
*/
protected $_eventPrefix = 'store';
/**
* Event object name
*
* @var string
*/
protected $_eventObject = 'store';
/**
* Price filter
*
* @var \Magento\Directory\Model\Currency\Filter
*/
protected $_priceFilter;
/**
* Store configuration cache
*
* @var array|null
*/
protected $_configCache = null;
/**
* Base nodes of store configuration cache
*
* @var array
*/
protected $_configCacheBaseNodes = [];
/**
* Directory cache
*
* @var array
*/
protected $_dirCache = [];
/**
* URL cache
*
* @var array
*/
protected $_urlCache = [];
/**
* Base URL cache
*
* @var array
*/
protected $_baseUrlCache = [];
/**
* Session entity
*
* @var \Magento\Framework\Session\SessionManagerInterface
*/
protected $_session;
/**
* Flag that shows that backend URLs are secure
*
* @var boolean|null
*/
protected $_isAdminSecure = null;
/**
* Flag that shows that frontend URLs are secure
*
* @var boolean|null
*/
protected $_isFrontSecure = null;
/**
* Store frontend name
*
* @var string|null
*/
protected $_frontendName = null;
/**
* Readonly flag
*
* @var bool
*/
private $_isReadOnly = false;
/**
* Url model for current store
*
* @var \Magento\Framework\UrlInterface
*/
protected $_url;
/**
* @var bool
*/
protected $_isCustomEntryPoint = false;
/**
* @var \Magento\Framework\App\RequestInterface
*/
protected $_request;
/**
* @var \Magento\Config\Model\ResourceModel\Config\Data
*/
protected $_configDataResource;
/**
* Core file storage database
*
* @var \Magento\MediaStorage\Helper\File\Storage\Database
*/
protected $_coreFileStorageDatabase = null;
/**
* Filesystem instance
*
* @var Filesystem
*/
protected $filesystem;
/**
* Store Config
*
* @var \Magento\Framework\App\Config\ReinitableConfigInterface
*/
protected $_config;
/**
* @var \Magento\Framework\Session\SidResolverInterface
*/
protected $_sidResolver;
/**
* @var string
*/
protected $_currencyInstalled;
/**
* @var \Magento\Framework\App\Http\Context
*/
protected $_httpContext;
/**
* @var \Magento\Directory\Model\CurrencyFactory
*/
protected $currencyFactory;
/**
* @var \Magento\Store\Api\GroupRepositoryInterface
*/
protected $groupRepository;
/**
* @var \Magento\Store\Api\WebsiteRepositoryInterface
*/
protected $websiteRepository;
/**
* @var \Magento\Store\Model\Information
*/
protected $information;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
* @param \Magento\Store\Model\ResourceModel\Store $resource
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase
* @param \Magento\Framework\App\Cache\Type\Config $configCacheType
* @param \Magento\Framework\UrlInterface $url
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Config\Model\ResourceModel\Config\Data $configDataResource
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
* @param \Magento\Framework\App\Http\Context $httpContext
* @param \Magento\Framework\Session\SessionManagerInterface $session
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
* @param Information $information
* @param string $currencyInstalled
* @param \Magento\Store\Api\GroupRepositoryInterface $groupRepository
* @param \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param bool $isCustomEntryPoint
* @param array $data optional generic object data
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Store\Model\ResourceModel\Store $resource,
\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase,
\Magento\Framework\App\Cache\Type\Config $configCacheType,
\Magento\Framework\UrlInterface $url,
\Magento\Framework\App\RequestInterface $request,
\Magento\Config\Model\ResourceModel\Config\Data $configDataResource,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\App\Config\ReinitableConfigInterface $config,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Session\SidResolverInterface $sidResolver,
\Magento\Framework\App\Http\Context $httpContext,
\Magento\Framework\Session\SessionManagerInterface $session,
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
\Magento\Store\Model\Information $information,
$currencyInstalled,
\Magento\Store\Api\GroupRepositoryInterface $groupRepository,
\Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
$isCustomEntryPoint = false,
array $data = []
) {
$this->_coreFileStorageDatabase = $coreFileStorageDatabase;
$this->_config = $config;
$this->_url = $url;
$this->_configCacheType = $configCacheType;
$this->_request = $request;
$this->_configDataResource = $configDataResource;
$this->_isCustomEntryPoint = $isCustomEntryPoint;
$this->filesystem = $filesystem;
$this->_storeManager = $storeManager;
$this->_sidResolver = $sidResolver;
$this->_httpContext = $httpContext;
$this->_session = $session;
$this->currencyFactory = $currencyFactory;
$this->information = $information;
$this->_currencyInstalled = $currencyInstalled;
$this->groupRepository = $groupRepository;
$this->websiteRepository = $websiteRepository;
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$resource,
$resourceCollection,
$data
);
}
/**
* @return string[]
*/
public function __sleep()
{
$properties = parent::__sleep();
$properties = array_diff($properties, ['_coreFileStorageDatabase', '_config']);
return $properties;
}
/**
* Init not serializable fields
*
* @return void
*/
public function __wakeup()
{
parent::__wakeup();
$this->_coreFileStorageDatabase = ObjectManager::getInstance()
->get('Magento\MediaStorage\Helper\File\Storage\Database');
$this->_config = ObjectManager::getInstance()->get('Magento\Framework\App\Config\ReinitableConfigInterface');
}
/**
* Initialize object
*
* @return void
*/
protected function _construct()
{
$this->_init('Magento\Store\Model\ResourceModel\Store');
}
/**
* Retrieve store session object
*
* @return \Magento\Framework\Session\SessionManagerInterface
*/
protected function _getSession()
{
if (!$this->_session->isSessionExists()) {
$this->_session->setName('store_' . $this->getCode());
$this->_session->start();
}
return $this->_session;
}
/**
* Validation rules for store
*
* @return \Zend_Validate_Interface|null
*/
protected function _getValidationRulesBeforeSave()
{
$validator = new \Magento\Framework\Validator\DataObject();
$storeLabelRule = new \Zend_Validate_NotEmpty();
$storeLabelRule->setMessage(__('Name is required'), \Zend_Validate_NotEmpty::IS_EMPTY);
$validator->addRule($storeLabelRule, 'name');
$storeCodeRule = new \Zend_Validate_Regex('/^[a-z]+[a-z0-9_]*$/');
$storeCodeRule->setMessage(
__(
'The store code may contain only letters (a-z), numbers (0-9) or underscore (_),'
. ' and the first character must be a letter.'
),
\Zend_Validate_Regex::NOT_MATCH
);
$validator->addRule($storeCodeRule, 'code');
return $validator;
}
/**
* Loading store data
*
* @param mixed $key
* @param string $field
* @return $this
*/
public function load($key, $field = null)
{
if (!is_numeric($key) && $field === null) {
$this->_getResource()->load($this, $key, 'code');
return $this;
}
return parent::load($key, $field);
}
/**
* Retrieve Store code
*
* @return string
*/
public function getCode()
{
return $this->_getData('code');
}
/**
* @inheritdoc
*/
public function setCode($code)
{
return $this->setData('code', $code);
}
/**
* @inheritdoc
*/
public function getName()
{
return $this->_getData('name');
}
/**
* @inheritdoc
*/
public function setName($name)
{
return $this->setData('name', $name);
}
/**
* Retrieve store configuration data
*
* @param string $path
* @return string|null
*/
public function getConfig($path)
{
$data = $this->_config->getValue($path, ScopeInterface::SCOPE_STORE, $this->getCode());
if (!$data) {
$data = $this->_config->getValue($path, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
}
return $data === false ? null : $data;
}
/**
* Set relation to the website
*
* @param Website $website
* @return void
*/
public function setWebsite(Website $website)
{
$this->setWebsiteId($website->getId());
}
/**
* Retrieve store website
*
* @return Website|bool
*/
public function getWebsite()
{
if ($this->getWebsiteId() === null) {
return false;
}
return $this->websiteRepository->getById($this->getWebsiteId());
}
/**
* Retrieve url using store configuration specific
*
* @param string $route
* @param array $params
* @return string
*/
public function getUrl($route = '', $params = [])
{
/** @var $url UrlInterface */
$url = $this->_url->setScope($this);
if ($this->_storeManager->getStore()->getId() != $this->getId()) {
$params['_scope_to_url'] = true;
}
return $url->getUrl($route, $params);
}
/**
* Retrieve base URL
*
* @param string $type
* @param boolean|null $secure
* @return string
* @throws \InvalidArgumentException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function getBaseUrl($type = UrlInterface::URL_TYPE_LINK, $secure = null)
{
$cacheKey = $type . '/' . ($secure === null ? 'null' : ($secure ? 'true' : 'false'));
if (!isset($this->_baseUrlCache[$cacheKey])) {
$secure = $secure === null ? $this->isCurrentlySecure() : (bool)$secure;
switch ($type) {
case UrlInterface::URL_TYPE_WEB:
$path = $secure
? self::XML_PATH_SECURE_BASE_URL
: self::XML_PATH_UNSECURE_BASE_URL;
$url = $this->getConfig($path);
break;
case UrlInterface::URL_TYPE_LINK:
$path = $secure ? self::XML_PATH_SECURE_BASE_LINK_URL : self::XML_PATH_UNSECURE_BASE_LINK_URL;
$url = $this->getConfig($path);
$url = $this->_updatePathUseRewrites($url);
$url = $this->_updatePathUseStoreView($url);
break;
case UrlInterface::URL_TYPE_DIRECT_LINK:
$path = $secure ? self::XML_PATH_SECURE_BASE_LINK_URL : self::XML_PATH_UNSECURE_BASE_LINK_URL;
$url = $this->getConfig($path);
$url = $this->_updatePathUseRewrites($url);
break;
case UrlInterface::URL_TYPE_STATIC:
$path = $secure ? self::XML_PATH_SECURE_BASE_STATIC_URL : self::XML_PATH_UNSECURE_BASE_STATIC_URL;
$url = $this->getConfig($path);
if (!$url) {
$url = $this->getBaseUrl(UrlInterface::URL_TYPE_WEB, $secure)
. $this->filesystem->getUri(DirectoryList::STATIC_VIEW);
}
break;
case UrlInterface::URL_TYPE_MEDIA:
$url = $this->_getMediaScriptUrl($this->filesystem, $secure);
if (!$url) {
$path = $secure ? self::XML_PATH_SECURE_BASE_MEDIA_URL : self::XML_PATH_UNSECURE_BASE_MEDIA_URL;
$url = $this->getConfig($path);
if (!$url) {
$url = $this->getBaseUrl(UrlInterface::URL_TYPE_WEB, $secure)
. $this->filesystem->getUri(DirectoryList::MEDIA);
}
}
break;
default:
throw new \InvalidArgumentException('Invalid base url type');
}
if (false !== strpos($url, self::BASE_URL_PLACEHOLDER)) {
$url = str_replace(self::BASE_URL_PLACEHOLDER, $this->_request->getDistroBaseUrl(), $url);
}
$this->_baseUrlCache[$cacheKey] = rtrim($url, '/') . '/';
}
return $this->_baseUrlCache[$cacheKey];
}
/**
* Retrieve base media directory path
*
* @return string
*/
public function getBaseMediaDir()
{
return $this->filesystem->getUri(DirectoryList::MEDIA);
}
/**
* Retrieve base static directory path
*
* @return string
*/
public function getBaseStaticDir()
{
return $this->filesystem->getUri(DirectoryList::STATIC_VIEW);
}
/**
* Append script file name to url in case when server rewrites are disabled
*
* @param string $url
* @return string
*/
protected function _updatePathUseRewrites($url)
{
if ($this->getForceDisableRewrites() || !$this->getConfig(self::XML_PATH_USE_REWRITES)) {
if ($this->_isCustomEntryPoint()) {
$indexFileName = 'index.php';
} else {
$indexFileName = basename($_SERVER['SCRIPT_FILENAME']);
}
$url .= $indexFileName . '/';
}
return $url;
}
/**
* Check if used entry point is custom
*
* @return bool
*/
protected function _isCustomEntryPoint()
{
return $this->_isCustomEntryPoint;
}
/**
* Retrieve URL for media catalog
*
* If we use Database file storage and server doesn't support rewrites (.htaccess in media folder)
* we have to put name of fetching media script exactly into URL
*
* @param Filesystem $filesystem
* @param bool $secure
* @return string|bool
*/
protected function _getMediaScriptUrl(Filesystem $filesystem, $secure)
{
if (!$this->getConfig(self::XML_PATH_USE_REWRITES) && $this->_coreFileStorageDatabase->checkDbUsage()) {
$baseUrl = $this->getBaseUrl(UrlInterface::URL_TYPE_WEB, $secure);
return $baseUrl . $filesystem->getUri(DirectoryList::PUB) . '/' . self::MEDIA_REWRITE_SCRIPT;
}
return false;
}
/**
* Add store code to url in case if it is enabled in configuration
*
* @param string $url
* @return string
*/
protected function _updatePathUseStoreView($url)
{
if ($this->isUseStoreInUrl()) {
$url .= $this->getCode() . '/';
}
return $url;
}
/**
* Returns whether url forming scheme prepends url path with store view code
*
* @return boolean
*/
public function isUseStoreInUrl()
{
return !($this->hasDisableStoreInUrl() && $this->getDisableStoreInUrl())
&& $this->getConfig(self::XML_PATH_STORE_IN_URL);
}
/**
* Get store identifier
*
* @return int
*/
public function getId()
{
return $this->_getData('store_id');
}
/**
* Check if frontend URLs should be secure
*
* @return boolean
*/
public function isFrontUrlSecure()
{
if ($this->_isFrontSecure === null) {
$this->_isFrontSecure = $this->_config
->isSetFlag(self::XML_PATH_SECURE_IN_FRONTEND, ScopeInterface::SCOPE_STORE, $this->getId());
}
return $this->_isFrontSecure;
}
/**
* @return bool
*/
public function isUrlSecure()
{
return $this->isFrontUrlSecure();
}
/**
* Check if request was secure
*
* @return boolean
*/
public function isCurrentlySecure()
{
if ($this->_request->isSecure()) {
return true;
}
$secureBaseUrl = $this->_config->getValue(self::XML_PATH_SECURE_BASE_URL, ScopeInterface::SCOPE_STORE);
$secureFrontend = $this->_config->getValue(self::XML_PATH_SECURE_IN_FRONTEND, ScopeInterface::SCOPE_STORE);
if (!$secureBaseUrl || !$secureFrontend) {
return false;
}
$uri = \Zend_Uri::factory($secureBaseUrl);
$port = $uri->getPort();
$serverPort = $this->_request->getServer('SERVER_PORT');
$isSecure = $uri->getScheme() == 'https' && isset($serverPort) && $port == $serverPort;
return $isSecure;
}
/*************************************************************************************
* Store currency interface
*/
/**
* Retrieve store base currency code
*
* @return string
*/
public function getBaseCurrencyCode()
{
$configValue = $this->getConfig(self::XML_PATH_PRICE_SCOPE);
if ($configValue == self::PRICE_SCOPE_GLOBAL) {
return $this->_config->getValue(Currency::XML_PATH_CURRENCY_BASE, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
}
return $this->getConfig(Currency::XML_PATH_CURRENCY_BASE);
}
/**
* Retrieve store base currency
*
* @return Currency
*/
public function getBaseCurrency()
{
$currency = $this->getData('base_currency');
if (null === $currency) {
$currency = $this->currencyFactory->create()->load($this->getBaseCurrencyCode());
$this->setData('base_currency', $currency);
}
return $currency;
}
/**
* Get default store currency code
*
* @return string
*/
public function getDefaultCurrencyCode()
{
$result = $this->getConfig(Currency::XML_PATH_CURRENCY_DEFAULT);
return $result;
}
/**
* Retrieve store default currency
*
* @return Currency
*/
public function getDefaultCurrency()
{
$currency = $this->getData('default_currency');
if (null === $currency) {
$currency = $this->currencyFactory->create()->load($this->getDefaultCurrencyCode());
$this->setData('default_currency', $currency);
}
return $currency;
}
/**
* Set current store currency code
*
* @param string $code
* @return string
*/
public function setCurrentCurrencyCode($code)
{
$code = strtoupper($code);
if (in_array($code, $this->getAvailableCurrencyCodes())) {
$this->_getSession()->setCurrencyCode($code);
$defaultCode = $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode();
$this->_httpContext->setValue(Context::CONTEXT_CURRENCY, $code, $defaultCode);
}
return $this;
}
/**
* Get current store currency code
*
* @return string
*/
public function getCurrentCurrencyCode()
{
// try to get currently set code among allowed
$code = $this->_httpContext->getValue(Context::CONTEXT_CURRENCY);
$code = $code === null ? $this->_getSession()->getCurrencyCode() : $code;
if (empty($code)) {
$code = $this->getDefaultCurrencyCode();
}
if (in_array($code, $this->getAvailableCurrencyCodes(true))) {
return $code;
}
// take first one of allowed codes
$codes = array_values($this->getAvailableCurrencyCodes(true));
if (empty($codes)) {
// return default code, if no codes specified at all
return $this->getDefaultCurrencyCode();
}
return array_shift($codes);
}
/**
* Get allowed store currency codes
*
* If base currency is not allowed in current website config scope,
* then it can be disabled with $skipBaseNotAllowed
*
* @param bool $skipBaseNotAllowed
* @return array
*/
public function getAvailableCurrencyCodes($skipBaseNotAllowed = false)
{
$codes = $this->getData('available_currency_codes');
if (null === $codes) {
$codes = explode(',', $this->getConfig(Currency::XML_PATH_CURRENCY_ALLOW));
// add base currency, if it is not in allowed currencies
$baseCurrencyCode = $this->getBaseCurrencyCode();
if (!in_array($baseCurrencyCode, $codes)) {
$codes[] = $baseCurrencyCode;
// save base currency code index for further usage
$disallowedBaseCodeIndex = array_keys($codes);
$disallowedBaseCodeIndex = array_pop($disallowedBaseCodeIndex);
$this->setData('disallowed_base_currency_code_index', $disallowedBaseCodeIndex);
}
$this->setData('available_currency_codes', $codes);
}
// remove base currency code, if it is not allowed by config (optional)
if ($skipBaseNotAllowed) {
$disallowedBaseCodeIndex = $this->getData('disallowed_base_currency_code_index');
if (null !== $disallowedBaseCodeIndex) {
unset($codes[$disallowedBaseCodeIndex]);
}
}
return $codes;
}
/**
* Array of installed currencies for the scope
*
* @return array
*/
public function getAllowedCurrencies()
{
return explode(',', $this->getConfig($this->_currencyInstalled));
}
/**
* Retrieve store current currency
*
* @return Currency
*/
public function getCurrentCurrency()
{
$currency = $this->getData('current_currency');
if ($currency === null) {
$currency = $this->currencyFactory->create()->load($this->getCurrentCurrencyCode());
$baseCurrency = $this->getBaseCurrency();
if (!$baseCurrency->getRate($currency)) {
$currency = $baseCurrency;
$this->setCurrentCurrencyCode($baseCurrency->getCode());
}
$this->setData('current_currency', $currency);
}
return $currency;
}
/**
* Retrieve current currency rate
*
* @return float
*/
public function getCurrentCurrencyRate()
{
return $this->getBaseCurrency()->getRate($this->getCurrentCurrency());
}
/**
* Retrieve root category identifier
*
* @return int
*/
public function getRootCategoryId()
{
if (!$this->getGroup()) {
return Category::ROOT_CATEGORY_ID;
}
return $this->getGroup()->getRootCategoryId();
}
/**
* Set group model for store
*
* @param Group $group
* @return Store
*/
public function setGroup(Group $group)
{
$this->setGroupId($group->getId());
return $this;
}
/**
* Retrieve group model
*
* @return Group|bool
*/
public function getGroup()
{
if (null === $this->getGroupId()) {
return false;