Skip to content

Commit

Permalink
Merge pull request #2 from opengento/hk2022
Browse files Browse the repository at this point in the history
👷 👷
  • Loading branch information
mehdichaouch authored Apr 1, 2022
2 parents d4b3253 + c1ff8ae commit 0bb8711
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 154 deletions.
30 changes: 16 additions & 14 deletions Controller/Api/Meteo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@

namespace Opengento\Snowflake\Controller\Api;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Opengento\Snowflake\Service\OpenWeatherMapApi;

class Meteo extends Action
class Meteo implements HttpGetActionInterface
{
protected OpenWeatherMapApi $api;
protected JsonFactory $resultJsonFactory;
private RequestInterface $request;

private OpenWeatherMapApi $api;

private JsonFactory $resultJsonFactory;

public function __construct(
Context $context,
RequestInterface $request,
OpenWeatherMapApi $api,
JsonFactory $resultJsonFactory
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->request = $request;
$this->api = $api;
$this->resultJsonFactory = $resultJsonFactory;
}

/**
* @throws \Cmfcmf\OpenWeatherMap\Exception
*/
public function execute()
// ToDo: Add exception handling + CSRF
public function execute(): Json
{
$isSnowing = ['is_snowing' => '0'];

$lat = $this->getRequest()->getParam('lat') ?? '';
$lon = $this->getRequest()->getParam('lon') ?? '';
$lat = $this->request->getParam('lat') ?? '';
$lon = $this->request->getParam('lon') ?? '';

if ($lat && $lon) {
$isSnowing = ['is_snowing' => $this->api->isSnowing($lat, $lon)];
Expand Down
12 changes: 4 additions & 8 deletions Model/Config/Backend/EmojiConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@

class EmojiConverter extends Value implements ProcessorInterface
{
/**
* Unset array element with '__empty' key
*
* @return $this
*/
public function beforeSave()
public function beforeSave(): self
{
$this->setValue(json_encode($this->getValue()));

return parent::beforeSave();
}

public function processValue($value)
public function processValue($value): string
{
return json_decode($value);
return (string)json_decode($value);
}
}
55 changes: 55 additions & 0 deletions Model/Config/OpenWeather.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Snowflake\Model\Config;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

/**
* @api
*/
final class OpenWeather
{
private const CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLED = 'snowflake/api/enable';
private const CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_API_KEY = 'snowflake/api/api_key';
private const CONFIG_PATH_SNOWFLAKE_IP_LOCATOR_API_URL = 'snowflake/api/ip_info_api_url';

private ScopeConfigInterface $scopeConfig;

public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

public function isEnabled(?int $scopeId = null): bool
{
return $this->scopeConfig->isSetFlag(
self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLED,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function getApiKey(?int $scopeId = null): string
{
return (string)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_API_KEY,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function getIpLocatorApiUrl(?int $scopeId = null): string
{
return (string)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_IP_LOCATOR_API_URL,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}
}
78 changes: 41 additions & 37 deletions Model/Config/Snowflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Snowflake
/**
* @api
*/
final class Snowflake
{
public const AJAX_URL = 'opengento_snowflake/api/meteo';

private const CONFIG_PATH_SNOWFLAKE_ENABLE = 'snowflake/general/enable';
private const CONFIG_PATH_SNOWFLAKE_CHAR = 'snowflake/general/icon';
private const CONFIG_PATH_SNOWFLAKE_V_SPEED = 'snowflake/general/vspeed';
private const CONFIG_PATH_SNOWFLAKE_H_SPEED = 'snowflake/general/hspeed';
private const CONFIG_PATH_SNOWFLAKE_ROT_SPEED = 'snowflake/general/rotspeed';
private const CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLE = 'snowflake/general/enable_api';
private const CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_API_KEY = 'snowflake/general/api_key';
private const CONFIG_PATH_SNOWFLAKE_MIN_SIZE = 'snowflake/general/max_size';
private const CONFIG_PATH_SNOWFLAKE_MAX_SIZE = 'snowflake/general/min_size';
private const CONFIG_PATH_SNOWFLAKE_QTY = 'snowflake/general/qty';
Expand All @@ -40,64 +39,69 @@ public function isEnabled(?int $scopeId = null): bool

public function getSnowflakeChar(?int $scopeId = null): string
{
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_CHAR, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
}

public function getSnowflakeVSpeed(?int $scopeId = null): string
{
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_V_SPEED, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
}

public function getSnowflakeHSpeed(?int $scopeId = null): string
{
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_H_SPEED, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
}

public function getSnowflakeRotSpeed(?int $scopeId = null): string
{
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_ROT_SPEED, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
return (string)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_CHAR,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function getSnowflakeQty(?int $scopeId = null): string
public function getSnowflakeVSpeed(?int $scopeId = null): float
{
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_QTY, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
return (float)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_V_SPEED,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function getSnowflakeMinSize(?int $scopeId = null): string
public function getSnowflakeHSpeed(?int $scopeId = null): float
{
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_MIN_SIZE, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
return (float)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_H_SPEED,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function getSnowflakeMaxSize(?int $scopeId = null): string
public function getSnowflakeRotSpeed(?int $scopeId = null): int
{
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_MAX_SIZE, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
return (int)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_ROT_SPEED,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function isForceSnow(?int $scopeId = null): bool
public function getSnowflakeQty(?int $scopeId = null): int
{
return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_SNOWFLAKE_FORCE, ScopeInterface::SCOPE_STORE, $scopeId);
return (int)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_QTY,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function isApiEnable(?int $scopeId = null): bool
public function getSnowflakeMinSize(?int $scopeId = null): int
{
return $this->scopeConfig->isSetFlag(
self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_ENABLE,
return (int)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_MIN_SIZE,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function getApiKey(?int $scopeId = null): string
public function getSnowflakeMaxSize(?int $scopeId = null): int
{
return $this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_OPENWEATHERMAP_API_KEY,
return (int)$this->scopeConfig->getValue(
self::CONFIG_PATH_SNOWFLAKE_MAX_SIZE,
ScopeInterface::SCOPE_STORE,
$scopeId
);
}

public function getAjaxUrl(): string
public function isForceSnow(?int $scopeId = null): bool
{
return static::AJAX_URL;
return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_SNOWFLAKE_FORCE, ScopeInterface::SCOPE_STORE, $scopeId);
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ bin/magento setup:upgrade

## Features

![Let it snow!](doc/snowflake.png)

### Snowflake base on local meteo

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!
Expand All @@ -59,6 +61,10 @@ You don't like snow, no problem, Snowflake can make it rain [cookies](https://tw

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

![General Config](doc/config1.png)

![Openweather Config](doc/config2.png)

## Support

Raise a new [request](https://github.com/opengento/magento2-snowflake/issues) to the issue tracker.
Expand Down
28 changes: 14 additions & 14 deletions Service/OpenWeatherMapApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@
use Cmfcmf\OpenWeatherMap;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Http\Factory\Guzzle\RequestFactory;
use Opengento\Snowflake\Model\Config\Snowflake;
use Opengento\Snowflake\Model\Config\OpenWeather as OpenWeatherConfig;

/**
* @api
*/
class OpenWeatherMapApi
{
// Manage all Group 6xx: Snow
// https://openweathermap.org/weather-conditions
/** Manage all Group 6xx: Snow: https://openweathermap.org/weather-conditions */
public const WEATHER_CONDITION = 'snow';

// Language of data (try your own language here!):
protected string $lang = 'en';
/** Language of data (try your own language here!):*/
private string $lang = 'en';

// Units (can be 'metric' or 'imperial' [default]):
protected string $units = 'metric';
/** Units (can be 'metric' or 'imperial' [default]):*/
private string $units = 'metric';

protected Snowflake $config;
private OpenWeatherConfig $openWeatherConfig;

public function __construct(
Snowflake $config
OpenWeatherConfig $openWeatherConfig
) {
$this->config = $config;
$this->openWeatherConfig = $openWeatherConfig;
}

/**
* @throws OpenWeatherMap\Exception
*/
public function isSnowing(string $lat, string $lon): bool
{
$apiKey = $this->config->getApiKey();

$httpRequestFactory = new RequestFactory();
$httpClient = GuzzleAdapter::createWithConfig([]);

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

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

return static::WEATHER_CONDITION === $weather->weather->description;
return self::WEATHER_CONDITION === $weather->weather->description;
}
}
Loading

0 comments on commit 0bb8711

Please sign in to comment.