Skip to content

Commit 0bb8711

Browse files
authored
Merge pull request #2 from opengento/hk2022
👷 👷
2 parents d4b3253 + c1ff8ae commit 0bb8711

File tree

19 files changed

+265
-154
lines changed

19 files changed

+265
-154
lines changed

Controller/Api/Meteo.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,37 @@
77

88
namespace Opengento\Snowflake\Controller\Api;
99

10-
use Magento\Framework\App\Action\Action;
11-
use Magento\Framework\App\Action\Context;
10+
use Magento\Framework\App\Action\HttpGetActionInterface;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Controller\Result\Json;
1213
use Magento\Framework\Controller\Result\JsonFactory;
1314
use Opengento\Snowflake\Service\OpenWeatherMapApi;
1415

15-
class Meteo extends Action
16+
class Meteo implements HttpGetActionInterface
1617
{
17-
protected OpenWeatherMapApi $api;
18-
protected JsonFactory $resultJsonFactory;
18+
private RequestInterface $request;
19+
20+
private OpenWeatherMapApi $api;
21+
22+
private JsonFactory $resultJsonFactory;
1923

2024
public function __construct(
21-
Context $context,
25+
RequestInterface $request,
2226
OpenWeatherMapApi $api,
2327
JsonFactory $resultJsonFactory
2428
) {
25-
parent::__construct($context);
26-
$this->resultJsonFactory = $resultJsonFactory;
29+
$this->request = $request;
2730
$this->api = $api;
31+
$this->resultJsonFactory = $resultJsonFactory;
2832
}
2933

30-
/**
31-
* @throws \Cmfcmf\OpenWeatherMap\Exception
32-
*/
33-
public function execute()
34+
// ToDo: Add exception handling + CSRF
35+
public function execute(): Json
3436
{
3537
$isSnowing = ['is_snowing' => '0'];
3638

37-
$lat = $this->getRequest()->getParam('lat') ?? '';
38-
$lon = $this->getRequest()->getParam('lon') ?? '';
39+
$lat = $this->request->getParam('lat') ?? '';
40+
$lon = $this->request->getParam('lon') ?? '';
3941

4042
if ($lat && $lon) {
4143
$isSnowing = ['is_snowing' => $this->api->isSnowing($lat, $lon)];

Model/Config/Backend/EmojiConverter.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@
1212

1313
class EmojiConverter extends Value implements ProcessorInterface
1414
{
15-
/**
16-
* Unset array element with '__empty' key
17-
*
18-
* @return $this
19-
*/
20-
public function beforeSave()
15+
public function beforeSave(): self
2116
{
2217
$this->setValue(json_encode($this->getValue()));
18+
2319
return parent::beforeSave();
2420
}
2521

26-
public function processValue($value)
22+
public function processValue($value): string
2723
{
28-
return json_decode($value);
24+
return (string)json_decode($value);
2925
}
3026
}

Model/Config/OpenWeather.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\Snowflake\Model\Config;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
/**
14+
* @api
15+
*/
16+
final class OpenWeather
17+
{
18+
private const CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLED = 'snowflake/api/enable';
19+
private const CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_API_KEY = 'snowflake/api/api_key';
20+
private const CONFIG_PATH_SNOWFLAKE_IP_LOCATOR_API_URL = 'snowflake/api/ip_info_api_url';
21+
22+
private ScopeConfigInterface $scopeConfig;
23+
24+
public function __construct(ScopeConfigInterface $scopeConfig)
25+
{
26+
$this->scopeConfig = $scopeConfig;
27+
}
28+
29+
public function isEnabled(?int $scopeId = null): bool
30+
{
31+
return $this->scopeConfig->isSetFlag(
32+
self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLED,
33+
ScopeInterface::SCOPE_STORE,
34+
$scopeId
35+
);
36+
}
37+
38+
public function getApiKey(?int $scopeId = null): string
39+
{
40+
return (string)$this->scopeConfig->getValue(
41+
self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_API_KEY,
42+
ScopeInterface::SCOPE_STORE,
43+
$scopeId
44+
);
45+
}
46+
47+
public function getIpLocatorApiUrl(?int $scopeId = null): string
48+
{
49+
return (string)$this->scopeConfig->getValue(
50+
self::CONFIG_PATH_SNOWFLAKE_IP_LOCATOR_API_URL,
51+
ScopeInterface::SCOPE_STORE,
52+
$scopeId
53+
);
54+
}
55+
}

Model/Config/Snowflake.php

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010
use Magento\Framework\App\Config\ScopeConfigInterface;
1111
use Magento\Store\Model\ScopeInterface;
1212

13-
class Snowflake
13+
/**
14+
* @api
15+
*/
16+
final class Snowflake
1417
{
15-
public const AJAX_URL = 'opengento_snowflake/api/meteo';
16-
1718
private const CONFIG_PATH_SNOWFLAKE_ENABLE = 'snowflake/general/enable';
1819
private const CONFIG_PATH_SNOWFLAKE_CHAR = 'snowflake/general/icon';
1920
private const CONFIG_PATH_SNOWFLAKE_V_SPEED = 'snowflake/general/vspeed';
2021
private const CONFIG_PATH_SNOWFLAKE_H_SPEED = 'snowflake/general/hspeed';
2122
private const CONFIG_PATH_SNOWFLAKE_ROT_SPEED = 'snowflake/general/rotspeed';
22-
private const CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLE = 'snowflake/general/enable_api';
23-
private const CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_API_KEY = 'snowflake/general/api_key';
2423
private const CONFIG_PATH_SNOWFLAKE_MIN_SIZE = 'snowflake/general/max_size';
2524
private const CONFIG_PATH_SNOWFLAKE_MAX_SIZE = 'snowflake/general/min_size';
2625
private const CONFIG_PATH_SNOWFLAKE_QTY = 'snowflake/general/qty';
@@ -40,64 +39,69 @@ public function isEnabled(?int $scopeId = null): bool
4039

4140
public function getSnowflakeChar(?int $scopeId = null): string
4241
{
43-
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_CHAR, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
44-
}
45-
46-
public function getSnowflakeVSpeed(?int $scopeId = null): string
47-
{
48-
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_V_SPEED, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
49-
}
50-
51-
public function getSnowflakeHSpeed(?int $scopeId = null): string
52-
{
53-
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_H_SPEED, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
54-
}
55-
56-
public function getSnowflakeRotSpeed(?int $scopeId = null): string
57-
{
58-
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_ROT_SPEED, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
42+
return (string)$this->scopeConfig->getValue(
43+
self::CONFIG_PATH_SNOWFLAKE_CHAR,
44+
ScopeInterface::SCOPE_STORE,
45+
$scopeId
46+
);
5947
}
6048

61-
public function getSnowflakeQty(?int $scopeId = null): string
49+
public function getSnowflakeVSpeed(?int $scopeId = null): float
6250
{
63-
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_QTY, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
51+
return (float)$this->scopeConfig->getValue(
52+
self::CONFIG_PATH_SNOWFLAKE_V_SPEED,
53+
ScopeInterface::SCOPE_STORE,
54+
$scopeId
55+
);
6456
}
6557

66-
public function getSnowflakeMinSize(?int $scopeId = null): string
58+
public function getSnowflakeHSpeed(?int $scopeId = null): float
6759
{
68-
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_MIN_SIZE, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
60+
return (float)$this->scopeConfig->getValue(
61+
self::CONFIG_PATH_SNOWFLAKE_H_SPEED,
62+
ScopeInterface::SCOPE_STORE,
63+
$scopeId
64+
);
6965
}
7066

71-
public function getSnowflakeMaxSize(?int $scopeId = null): string
67+
public function getSnowflakeRotSpeed(?int $scopeId = null): int
7268
{
73-
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_MAX_SIZE, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
69+
return (int)$this->scopeConfig->getValue(
70+
self::CONFIG_PATH_SNOWFLAKE_ROT_SPEED,
71+
ScopeInterface::SCOPE_STORE,
72+
$scopeId
73+
);
7474
}
7575

76-
public function isForceSnow(?int $scopeId = null): bool
76+
public function getSnowflakeQty(?int $scopeId = null): int
7777
{
78-
return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_SNOWFLAKE_FORCE, ScopeInterface::SCOPE_STORE, $scopeId);
78+
return (int)$this->scopeConfig->getValue(
79+
self::CONFIG_PATH_SNOWFLAKE_QTY,
80+
ScopeInterface::SCOPE_STORE,
81+
$scopeId
82+
);
7983
}
8084

81-
public function isApiEnable(?int $scopeId = null): bool
85+
public function getSnowflakeMinSize(?int $scopeId = null): int
8286
{
83-
return $this->scopeConfig->isSetFlag(
84-
self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLE,
87+
return (int)$this->scopeConfig->getValue(
88+
self::CONFIG_PATH_SNOWFLAKE_MIN_SIZE,
8589
ScopeInterface::SCOPE_STORE,
8690
$scopeId
8791
);
8892
}
8993

90-
public function getApiKey(?int $scopeId = null): string
94+
public function getSnowflakeMaxSize(?int $scopeId = null): int
9195
{
92-
return $this->scopeConfig->getValue(
93-
self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_API_KEY,
96+
return (int)$this->scopeConfig->getValue(
97+
self::CONFIG_PATH_SNOWFLAKE_MAX_SIZE,
9498
ScopeInterface::SCOPE_STORE,
9599
$scopeId
96100
);
97101
}
98102

99-
public function getAjaxUrl(): string
103+
public function isForceSnow(?int $scopeId = null): bool
100104
{
101-
return static::AJAX_URL;
105+
return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_SNOWFLAKE_FORCE, ScopeInterface::SCOPE_STORE, $scopeId);
102106
}
103107
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ bin/magento setup:upgrade
4141

4242
## Features
4343

44+
![Let it snow!](doc/snowflake.png)
45+
4446
### Snowflake base on local meteo
4547

4648
Do you like the snow? Do you like to feel the same weather on your favorite website? Try this plugin and get the snow along your local meteo!
@@ -59,6 +61,10 @@ You don't like snow, no problem, Snowflake can make it rain [cookies](https://tw
5961

6062
The configuration for this module is available in `Stores > Configuration > General > ❄️ Snowflake`.
6163

64+
![General Config](doc/config1.png)
65+
66+
![Openweather Config](doc/config2.png)
67+
6268
## Support
6369

6470
Raise a new [request](https://github.com/opengento/magento2-snowflake/issues) to the issue tracker.

Service/OpenWeatherMapApi.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,42 @@
1010
use Cmfcmf\OpenWeatherMap;
1111
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
1212
use Http\Factory\Guzzle\RequestFactory;
13-
use Opengento\Snowflake\Model\Config\Snowflake;
13+
use Opengento\Snowflake\Model\Config\OpenWeather as OpenWeatherConfig;
1414

15+
/**
16+
* @api
17+
*/
1518
class OpenWeatherMapApi
1619
{
17-
// Manage all Group 6xx: Snow
18-
// https://openweathermap.org/weather-conditions
20+
/** Manage all Group 6xx: Snow: https://openweathermap.org/weather-conditions */
1921
public const WEATHER_CONDITION = 'snow';
2022

21-
// Language of data (try your own language here!):
22-
protected string $lang = 'en';
23+
/** Language of data (try your own language here!):*/
24+
private string $lang = 'en';
2325

24-
// Units (can be 'metric' or 'imperial' [default]):
25-
protected string $units = 'metric';
26+
/** Units (can be 'metric' or 'imperial' [default]):*/
27+
private string $units = 'metric';
2628

27-
protected Snowflake $config;
29+
private OpenWeatherConfig $openWeatherConfig;
2830

2931
public function __construct(
30-
Snowflake $config
32+
OpenWeatherConfig $openWeatherConfig
3133
) {
32-
$this->config = $config;
34+
$this->openWeatherConfig = $openWeatherConfig;
3335
}
3436

3537
/**
3638
* @throws OpenWeatherMap\Exception
3739
*/
3840
public function isSnowing(string $lat, string $lon): bool
3941
{
40-
$apiKey = $this->config->getApiKey();
41-
4242
$httpRequestFactory = new RequestFactory();
4343
$httpClient = GuzzleAdapter::createWithConfig([]);
4444

45-
$owm = new OpenWeatherMap($apiKey, $httpClient, $httpRequestFactory);
45+
$owm = new OpenWeatherMap($this->openWeatherConfig->getApiKey(), $httpClient, $httpRequestFactory);
4646

4747
$weather = $owm->getWeather(['lat' => $lat, 'lon' => $lon], $this->lang, $this->units);
4848

49-
return static::WEATHER_CONDITION === $weather->weather->description;
49+
return self::WEATHER_CONDITION === $weather->weather->description;
5050
}
5151
}

0 commit comments

Comments
 (0)