1
+ <?php
2
+
3
+ /** Copyright © Magefan (support@magefan.com).
4
+ * All rights reserved.
5
+ * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
6
+ */
7
+
8
+ namespace Magefan \Community \Model ;
9
+
10
+ use Magento \Framework \Config \ConfigOptionsListConstants ;
11
+
12
+ /**
13
+ * Class AdminNotificationFeedextends
14
+ * @package Magefan\Community\Model
15
+ */
16
+ class AdminNotificationFeedextends extends \Magento \AdminNotification \Model \Feed
17
+ {
18
+ /**
19
+ * @var \Magento\Backend\Model\Auth\Session
20
+ */
21
+ protected $ _backendAuthSession ;
22
+ /**
23
+ * @var \Magento\Framework\Module\ModuleListInterface
24
+ */
25
+ protected $ _moduleList ;
26
+ /**
27
+ * @var \Magento\Framework\Module\Manager
28
+ */
29
+ protected $ _moduleManager ;
30
+ /**
31
+ * @param \Magento\Framework\Model\Context $context
32
+ * @param \Magento\Framework\Registry $registry
33
+ * @param \Magento\Backend\App\ConfigInterface $backendConfig
34
+ * @param InboxFactory $inboxFactory
35
+ * @param \Magento\Backend\Model\Auth\Session $backendAuthSession
36
+ * @param \Magento\Framework\Module\ModuleListInterface $moduleList
37
+ * @param \Magento\Framework\Module\Manager $moduleManager,
38
+ * @param \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
39
+ * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
40
+ * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
41
+ * @param \Magento\Framework\UrlInterface $urlBuilder
42
+ * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
43
+ * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
44
+ * @param array $data
45
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
46
+ */
47
+ public function __construct (
48
+ \Magento \Framework \Model \Context $ context ,
49
+ \Magento \Framework \Registry $ registry ,
50
+ \Magento \Backend \App \ConfigInterface $ backendConfig ,
51
+ \Magento \AdminNotification \Model \InboxFactory $ inboxFactory ,
52
+ \Magento \Backend \Model \Auth \Session $ backendAuthSession ,
53
+ \Magento \Framework \Module \ModuleListInterface $ moduleList ,
54
+ \Magento \Framework \Module \Manager $ moduleManager ,
55
+ \Magento \Framework \HTTP \Adapter \CurlFactory $ curlFactory ,
56
+ \Magento \Framework \App \DeploymentConfig $ deploymentConfig ,
57
+ \Magento \Framework \App \ProductMetadataInterface $ productMetadata ,
58
+ \Magento \Framework \UrlInterface $ urlBuilder ,
59
+ \Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
60
+ \Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
61
+ array $ data = []
62
+ ) {
63
+ parent ::__construct ($ context , $ registry , $ backendConfig , $ inboxFactory , $ curlFactory , $ deploymentConfig , $ productMetadata , $ urlBuilder , $ resource , $ resourceCollection , $ data );
64
+ $ this ->_backendAuthSession = $ backendAuthSession ;
65
+ $ this ->_moduleList = $ moduleList ;
66
+ $ this ->_moduleManager = $ moduleManager ;
67
+ }
68
+
69
+ /**
70
+ * Retrieve feed url
71
+ *
72
+ * @return string
73
+ */
74
+ public function getFeedUrl ()
75
+ {
76
+ if (is_null ($ this ->_feedUrl )) {
77
+ $ this ->_feedUrl = 'http://mage ' .'fan '
78
+ .'.c ' .'om/community/notifications ' .'/ ' .'feed/ ' ;
79
+ }
80
+ $ urlInfo = parse_url ($ this ->urlBuilder ->getBaseUrl ());
81
+ $ domain = isset ($ urlInfo ['host ' ]) ? $ urlInfo ['host ' ] : '' ;
82
+ $ url = $ this ->_feedUrl . 'domain/ ' . urlencode ($ domain );
83
+ $ modulesParams = [];
84
+ foreach ($ this ->getMagefanModules () as $ key => $ module ) {
85
+ $ key = str_replace ('Magefan_ ' , '' , $ key );
86
+ $ modulesParams [] = $ key . ', ' . $ module ['setup_version ' ];
87
+ }
88
+ if (count ($ modulesParams )) {
89
+ $ url .= '/modules/ ' .base64_encode (implode ('; ' , $ modulesParams ));
90
+ }
91
+ return $ url ;
92
+ }
93
+
94
+ /**
95
+ * Get Magefan Modules Info
96
+ *
97
+ * @return $this
98
+ */
99
+ protected function getMagefanModules ()
100
+ {
101
+ $ modules = [];
102
+ foreach ($ this ->_moduleList ->getAll () as $ moduleName => $ module ) {
103
+ if (strpos ($ moduleName , 'Magefan_ ' ) !== false && $ this ->_moduleManager ->isEnabled ($ moduleName )) {
104
+ $ modules [$ moduleName ] = $ module ;
105
+ }
106
+ }
107
+ return $ modules ;
108
+ }
109
+
110
+ /**
111
+ * Check feed for modification
112
+ *
113
+ * @return $this
114
+ */
115
+ public function checkUpdate ()
116
+ {
117
+ $ session = $ this ->_backendAuthSession ;
118
+ $ time = time ();
119
+ $ frequency = $ this ->getFrequency ();
120
+ if (($ frequency + $ session ->getMfNoticeLastUpdate () > $ time )
121
+ || ($ frequency + $ this ->getLastUpdate () > $ time )
122
+ ) {
123
+ return $ this ;
124
+ }
125
+ $ session ->setMfNoticeLastUpdate ($ time );
126
+ return parent ::checkUpdate ();
127
+ }
128
+
129
+ /**
130
+ * Retrieve Update Frequency
131
+ *
132
+ * @return int
133
+ */
134
+ public function getFrequency ()
135
+ {
136
+ return 86400 ;
137
+ }
138
+
139
+ /**
140
+ * Retrieve Last update time
141
+ *
142
+ * @return int
143
+ */
144
+ public function getLastUpdate ()
145
+ {
146
+ return $ this ->_cacheManager ->load ('magefan_admin_notifications_lastcheck ' );
147
+ }
148
+
149
+ /**
150
+ * Set last update time (now)
151
+ *
152
+ * @return $this
153
+ */
154
+ public function setLastUpdate ()
155
+ {
156
+ $ this ->_cacheManager ->save (time (), 'magefan_admin_notifications_lastcheck ' );
157
+ return $ this ;
158
+ }
159
+ }
0 commit comments