Skip to content

Commit

Permalink
Merge pull request #4 from opengento/improvements
Browse files Browse the repository at this point in the history
Validate composer.json 👷 and more...
  • Loading branch information
mehdichaouch authored Mar 20, 2022
2 parents 29e3caf + 5867f10 commit d4b3253
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 157 deletions.
51 changes: 29 additions & 22 deletions Block/Snowflake.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Snowflake\Block;
Expand All @@ -9,62 +13,65 @@

class Snowflake extends Template
{
protected ScopeConfigInterface $snowflakeConfig;
protected SnowflakeConfig $config;
protected SnowflakeConfig $snowflakeConfig;
protected ScopeConfigInterface $scopeConfig;

public function __construct(
ScopeConfigInterface $scopeConfig,
SnowflakeConfig $snowflakeConfig,
Template\Context $context,
SnowflakeConfig $snowflakeConfig,
array $data = []
) {
$this->SnowflakeConfig = $snowflakeConfig;
$this->scopeConfig = $scopeConfig;
$this->snowflakeConfig = $snowflakeConfig;
parent::__construct($context, $data);
}

public function getSnowflakeChar(): string
{
return $this->SnowflakeConfig->getSnowflakeChar();
return $this->snowflakeConfig->getSnowflakeChar();
}

public function getSnowflakeVSpeed(): string
{
return $this->snowflakeConfig->getSnowflakeVSpeed();
}

public function getSnowflakeVSpeed() : string
public function getSnowflakeHSpeed(): string
{
return $this->SnowflakeConfig->getSnowflakeVSpeed();
return $this->snowflakeConfig->getSnowflakeHSpeed();
}

public function getSnowflakeHSpeed() : string
public function getSnowflakeRotSpeed(): string
{
return $this->SnowflakeConfig->getSnowflakeHSpeed();
return $this->snowflakeConfig->getSnowflakeRotSpeed();
}

public function getSnowflakeRotSpeed() : string
public function getSnowflakeQty(): int
{
return $this->SnowflakeConfig->getSnowflakeRotSpeed();
return (int)$this->snowflakeConfig->getSnowflakeQty();
}

public function getSnowflakeQty() : int
public function getSnowflakeMinSize(): int
{
return (int)$this->SnowflakeConfig->getSnowflakeQty();
return (int)$this->snowflakeConfig->getSnowflakeMinSize();
}

public function getSnowflakeMinSize() : int
public function getSnowflakeMaxSize(): int
{
return (int)$this->SnowflakeConfig->getSnowflakeMinSize();
return (int)$this->snowflakeConfig->getSnowflakeMaxSize();
}

public function getSnowflakeMaxSize() : int
public function isForceSnow(): bool
{
return (int)$this->SnowflakeConfig->getSnowflakeMaxSize();
return $this->snowflakeConfig->isForceSnow();
}

public function getSnowflakeForce() : string
public function isApiEnable(): bool
{
return $this->SnowflakeConfig->getSnowflakeForce();
return $this->snowflakeConfig->isApiEnable();
}

public function getAjaxUrl(): string
{
return $this->SnowflakeConfig->getAjaxUrl();
return $this->snowflakeConfig->getAjaxUrl();
}
}
16 changes: 10 additions & 6 deletions Controller/Index/Meteo.php → Controller/Api/Meteo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
declare(strict_types=1);

namespace Opengento\Snowflake\Controller\Index;
namespace Opengento\Snowflake\Controller\Api;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
Expand All @@ -32,11 +32,15 @@ public function __construct(
*/
public function execute()
{
$lat = $this->getRequest()->getParam('lat');
$lon = $this->getRequest()->getParam('lon');
$isSnowing = ['is_snowing' => '0'];

return $this->resultJsonFactory->create()->setData(
['is_snowing' => $this->api->isSnowing($lat, $lon)]
);
$lat = $this->getRequest()->getParam('lat') ?? '';
$lon = $this->getRequest()->getParam('lon') ?? '';

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

return $this->resultJsonFactory->create()->setData($isSnowing);
}
}
11 changes: 8 additions & 3 deletions Model/Config/Backend/EmojiConverter.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php
declare(strict_types = 1);
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Snowflake\Model\Config\Backend;

use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
use Magento\Framework\App\Config\Data\ProcessorInterface;
use Magento\Framework\App\Config\Value;

class EmojiConverter extends \Magento\Framework\App\Config\Value implements \Magento\Framework\App\Config\Data\ProcessorInterface
class EmojiConverter extends Value implements ProcessorInterface
{
/**
* Unset array element with '__empty' key
Expand Down
22 changes: 16 additions & 6 deletions Model/Config/Snowflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@

class Snowflake
{
public const AJAX_URL = 'opengento_snowflake/index/meteo';
public const AJAX_URL = 'opengento_snowflake/api/meteo';

private const CONFIG_PATH_SNOWFLAKE_ENABLE = 'opengento/snowflake/enable';
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_API_KEY = 'opengento/snowflake/api_key';
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';
private const CONFIG_PATH_SNOWFLAKE_FORCE = 'snowflake/api/force';
private const CONFIG_PATH_SNOWFLAKE_FORCE = 'snowflake/general/force';

private ScopeConfigInterface $scopeConfig;

Expand Down Expand Up @@ -72,9 +73,18 @@ public function getSnowflakeMaxSize(?int $scopeId = null): string
return $this->scopeConfig->getValue(self::CONFIG_PATH_SNOWFLAKE_MAX_SIZE, ScopeInterface::SCOPE_STORE, $scopeId) ?? '';
}

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

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

public function getApiKey(?int $scopeId = null): string
Expand Down
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
[![Packagist](https://img.shields.io/packagist/dt/opengento/module-snowflake.svg?style=flat-square)](https://packagist.org/packages/opengento/module-snowflake/stats)
[![Packagist](https://img.shields.io/packagist/dm/opengento/module-snowflake.svg?style=flat-square)](https://packagist.org/packages/opengento/module-snowflake/stats)

This module adds ❄️️ based on local meteo.
This fun module adds ❄️️ on your website all the time or based on your visitor outside weather.

- [Setup](#setup)
- [Composer installation](#composer-installation)
- [Setup the module](#setup-the-snowflake)
- [Setup the module](#setup-the-module)
- [Features](#features)
- [Settings](#settings)
- [Documentation](#documentation)
Expand All @@ -21,19 +21,19 @@ This module adds ❄️️ based on local meteo.

Magento 2 Open Source or Commerce edition is required.

### Composer installation
### Composer installation

Run the following composer command:

```
```shell
composer require opengento/module-snowflake
```

### Setup the module

Run the following magento command:

```
```shell
bin/magento setup:upgrade
```

Expand All @@ -45,21 +45,27 @@ bin/magento setup:upgrade

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!

## Settings
Require: [OpenWeatherMap API key](https://openweathermap.org/).

### Force Snowflake

The configuration for this module is available in `Stores > Configuration > General > Snowflake`.
It doesn't snow everywhere, so you can force snow.

## Documentation
### Hacking Snowflake

You don't like snow, no problem, Snowflake can make it rain [cookies](https://twitter.com/MehdiChch/status/1505649692755079169), [hot-dog](https://twitter.com/MehdiChch/status/1505258061249884160) or whatever you want.

## Settings

Enable the module in the configuration panel to enable the snowflake based on the local meteo.
The configuration for this module is available in `Stores > Configuration > General > ❄️ Snowflake`.

## Support

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

## Authors

- **Opengento Community** - *Lead* - [![Twitter Follow](https://img.shields.io/twitter/follow/opengento.svg?style=social)](https://twitter.com/opengento)
- **OpenGento Community** - *Lead* - [![Twitter Follow](https://img.shields.io/twitter/follow/opengento.svg?style=social)](https://twitter.com/opengento)
- **Contributors** - *Contributor* - [![GitHub contributors](https://img.shields.io/github/contributors/opengento/magento2-snowflake.svg?style=flat-square)](https://github.com/opengento/magento2-snowflake/graphs/contributors)

## License
Expand Down
8 changes: 6 additions & 2 deletions Service/OpenWeatherMapApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

class OpenWeatherMapApi
{
// 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';

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

Expand All @@ -42,6 +46,6 @@ public function isSnowing($lat, $lon): bool

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

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

0 comments on commit d4b3253

Please sign in to comment.