66
77namespace Magefan \GeoIp \Model ;
88
9+
910use Magento \Store \Model \ScopeInterface ;
1011use Magento \Framework \Filesystem \DirectoryList ;
1112use Magento \Framework \Module \Dir as ModuleDir ;
13+ use Magento \Framework \App \RequestInterface ;
14+ use Magento \Framework \App \Config \ScopeConfigInterface ;
15+ use Magento \Framework \HTTP \PhpEnvironment \RemoteAddress ;
1216
1317/**
1418 * Class IpToCountryRepository
@@ -32,27 +36,22 @@ class IpToCountryRepository
3236 const XML_PATH_SIMULATE_COUNTRY = 'mfgeoip/developer/simulate_country ' ;
3337
3438 /**
35- * @var \Magento\Framework\HTTP\PhpEnvironment\ RemoteAddress
39+ * @var RemoteAddress
3640 */
3741 protected $ remoteAddress ;
3842
39- /**
40- * @var ResourceModel\IpToCountry\CollectionFactory
41- */
42- protected $ ipToCountryCollectionFactory ;
43-
4443 /**
4544 * @var array
4645 */
4746 protected $ ipToCountry = [];
4847
4948 /**
50- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
49+ * @var ScopeConfigInterface
5150 */
5251 protected $ config ;
5352
5453 /**
55- * @var \Magento\Framework\App\ RequestInterface
54+ * @var RequestInterface
5655 */
5756 protected $ request ;
5857
@@ -68,33 +67,24 @@ class IpToCountryRepository
6867
6968 /**
7069 * IpToCountryRepository constructor.
71- * @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
72- * @param ResourceModel\IpToCountry\CollectionFactory $ipToCountryCollectionFactory
70+ * @param RemoteAddress $remoteAddress
7371 * @param DirectoryList $directoryList
7472 * @param ModuleDir $moduleDir
75- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $config
76- * @param \Magento\Framework\App\ RequestInterface $httpRequest
73+ * @param ScopeConfigInterface $config
74+ * @param RequestInterface $httpRequest
7775 */
7876 public function __construct (
79- \Magento \Framework \HTTP \PhpEnvironment \RemoteAddress $ remoteAddress ,
80- ResourceModel \IpToCountry \CollectionFactory $ ipToCountryCollectionFactory ,
77+ RemoteAddress $ remoteAddress ,
8178 DirectoryList $ directoryList ,
8279 ModuleDir $ moduleDir ,
83- $ config = null ,
84- $ httpRequest = null
80+ ScopeConfigInterface $ config ,
81+ RequestInterface $ httpRequest
8582 ) {
8683 $ this ->remoteAddress = $ remoteAddress ;
87- $ this ->ipToCountryCollectionFactory = $ ipToCountryCollectionFactory ;
8884 $ this ->directoryList = $ directoryList ;
8985 $ this ->moduleDir = $ moduleDir ;
90-
91- $ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
92- $ this ->config = $ config ?: $ objectManager ->get (
93- \Magento \Framework \App \Config \ScopeConfigInterface::class
94- );
95- $ this ->request = $ httpRequest ?: $ objectManager ->get (
96- \Magento \Framework \App \RequestInterface::class
97- );
86+ $ this ->config = $ config ;
87+ $ this ->request = $ httpRequest ;
9888 }
9989
10090 /**
@@ -157,16 +147,6 @@ public function getCountryCode($ip)
157147 }
158148 } catch (\Exception $ e ) {}
159149 }
160-
161- if (!$ this ->ipToCountry [$ ip ]) {
162- $ longIp = ip2long ($ ip );
163- $ collection = $ this ->ipToCountryCollectionFactory ->create ();
164- $ collection ->addFieldToFilter ('ip_from ' , ["lteq " => $ longIp ])
165- ->addFieldToFilter ('ip_to ' , ["gteq " => $ longIp ])
166- ->setPageSize (1 );
167- $ ipInfo = $ collection ->getFirstItem ();
168- $ this ->ipToCountry [$ ip ] = $ ipInfo ->getCountryCode () ?: false ;
169- }
170150 }
171151
172152 return $ this ->ipToCountry [$ ip ];
0 commit comments