add backend messages support
This commit is contained in:
parent
4111a0cfd8
commit
9c18634d63
4
composer.lock
generated
4
composer.lock
generated
@ -2953,7 +2953,7 @@
|
|||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "http://git.kadet.net/kadet/functional-php.git",
|
"url": "http://git.kadet.net/kadet/functional-php.git",
|
||||||
"reference": "024d97d46c183df8f3ccaf1f8b4dc73d9cb629ae"
|
"reference": "f8ea3de9f7f6d2f2427488434d73edd8967b6856"
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.1"
|
"php": ">=7.1"
|
||||||
@ -2982,7 +2982,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"description": "Functional library for PHP",
|
"description": "Functional library for PHP",
|
||||||
"time": "2018-08-27T16:57:36+00:00"
|
"time": "2018-09-02T18:26:49+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "symfony/dotenv",
|
"name": "symfony/dotenv",
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"webpack": "^4.17.0",
|
"webpack": "^4.17.0",
|
||||||
"webpack-cli": "^3.1.0"
|
"webpack-cli": "^3.1.0",
|
||||||
"@fortawesome/fontawesome-svg-core": "^1.2.4",
|
"@fortawesome/fontawesome-svg-core": "^1.2.4",
|
||||||
"@fortawesome/pro-light-svg-icons": "^5.3.1",
|
"@fortawesome/pro-light-svg-icons": "^5.3.1",
|
||||||
"@fortawesome/pro-regular-svg-icons": "^5.3.1",
|
"@fortawesome/pro-regular-svg-icons": "^5.3.1",
|
||||||
|
21
src/Controller/MessagesController.php
Normal file
21
src/Controller/MessagesController.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Provider\MessageRepository;
|
||||||
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/{provider}/messages")
|
||||||
|
*/
|
||||||
|
class MessagesController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Route("/", methods={"GET"})
|
||||||
|
*/
|
||||||
|
public function all(MessageRepository $messages)
|
||||||
|
{
|
||||||
|
return $this->json($messages->getAll());
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,8 @@
|
|||||||
namespace App\Model;
|
namespace App\Model;
|
||||||
|
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
class Message implements Fillable
|
class Message implements Fillable
|
||||||
{
|
{
|
||||||
use FillTrait;
|
use FillTrait;
|
||||||
@ -16,13 +18,25 @@ class Message implements Fillable
|
|||||||
* Message content.
|
* Message content.
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $message;
|
private $message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Message type, see TYPE_* constants
|
* Message type, see TYPE_* constants
|
||||||
* @var
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $type = self::TYPE_UNKNOWN;
|
private $type = self::TYPE_UNKNOWN;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message validity time span start
|
||||||
|
* @var Carbon|null
|
||||||
|
*/
|
||||||
|
private $validFrom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message validity time span end
|
||||||
|
* @var Carbon|null
|
||||||
|
*/
|
||||||
|
private $validTo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
@ -55,4 +69,36 @@ class Message implements Fillable
|
|||||||
{
|
{
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Carbon|null
|
||||||
|
*/
|
||||||
|
public function getValidFrom(): ?Carbon
|
||||||
|
{
|
||||||
|
return $this->validFrom;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon|null $validFrom
|
||||||
|
*/
|
||||||
|
public function setValidFrom(?Carbon $validFrom): void
|
||||||
|
{
|
||||||
|
$this->validFrom = $validFrom;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Carbon|null
|
||||||
|
*/
|
||||||
|
public function getValidTo(): ?Carbon
|
||||||
|
{
|
||||||
|
return $this->validTo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon|null $validTo
|
||||||
|
*/
|
||||||
|
public function setValidTo(?Carbon $validTo): void
|
||||||
|
{
|
||||||
|
$this->validTo = $validTo;
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,20 +4,59 @@
|
|||||||
namespace App\Provider\ZtmGdansk;
|
namespace App\Provider\ZtmGdansk;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Model\Message;
|
||||||
use App\Model\Stop;
|
use App\Model\Stop;
|
||||||
use App\Provider\MessageRepository;
|
use App\Provider\MessageRepository;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Symfony\Component\Cache\Adapter\AdapterInterface;
|
||||||
use Tightenco\Collect\Support\Collection;
|
use Tightenco\Collect\Support\Collection;
|
||||||
|
|
||||||
class ZtmGdanskMessageRepository implements MessageRepository
|
class ZtmGdanskMessageRepository implements MessageRepository
|
||||||
{
|
{
|
||||||
|
const MESSAGES_URL = "http://87.98.237.99:88/displayMessages";
|
||||||
|
|
||||||
|
private $cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ZtmGdanskStopRepository constructor.
|
||||||
|
*/
|
||||||
|
public function __construct(AdapterInterface $cache)
|
||||||
|
{
|
||||||
|
$this->cache = $cache;
|
||||||
|
}
|
||||||
|
|
||||||
public function getAll(): Collection
|
public function getAll(): Collection
|
||||||
{
|
{
|
||||||
return collect();
|
return collect($this->queryZtmApi())->unique(function ($message) {
|
||||||
|
return $message['messagePart1'] . $message['messagePart2'];
|
||||||
|
})->map(function ($message) {
|
||||||
|
return Message::createFromArray([
|
||||||
|
'message' => trim($message['messagePart1'] . $message['messagePart2']),
|
||||||
|
'type' => Message::TYPE_UNKNOWN,
|
||||||
|
'validFrom' => new Carbon($message['startDate']),
|
||||||
|
'validTo' => new Carbon($message['endDate']),
|
||||||
|
]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getForStop(Stop $stop): Collection
|
public function getForStop(Stop $stop): Collection
|
||||||
{
|
{
|
||||||
return collect();
|
return $this->getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function queryZtmApi()
|
||||||
|
{
|
||||||
|
$item = $this->cache->getItem('ztm-gdansk.messages');
|
||||||
|
|
||||||
|
if (!$item->isHit()) {
|
||||||
|
$messages = json_decode(file_get_contents(static::MESSAGES_URL), true);
|
||||||
|
|
||||||
|
$item->expiresAfter(60);
|
||||||
|
$item->set($messages['displaysMsg']);
|
||||||
|
|
||||||
|
$this->cache->save($item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $item->get();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ namespace App\Service;
|
|||||||
use App\Exception\NonExistentServiceException;
|
use App\Exception\NonExistentServiceException;
|
||||||
use App\Provider\DepartureRepository;
|
use App\Provider\DepartureRepository;
|
||||||
use App\Provider\LineRepository;
|
use App\Provider\LineRepository;
|
||||||
|
use App\Provider\MessageRepository;
|
||||||
use App\Provider\StopRepository;
|
use App\Provider\StopRepository;
|
||||||
use const Kadet\Functional\_;
|
use const Kadet\Functional\_;
|
||||||
use function Kadet\Functional\any;
|
use function Kadet\Functional\any;
|
||||||
@ -54,6 +55,10 @@ class RepositoryParameterConverter implements ParamConverterInterface
|
|||||||
$request->attributes->set($configuration->getName(), $provider->getDepartureRepository());
|
$request->attributes->set($configuration->getName(), $provider->getDepartureRepository());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case is_a($class, MessageRepository::class, true):
|
||||||
|
$request->attributes->set($configuration->getName(), $provider->getMessageRepository());
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -66,12 +71,13 @@ class RepositoryParameterConverter implements ParamConverterInterface
|
|||||||
|
|
||||||
public function supports(ParamConverter $configuration)
|
public function supports(ParamConverter $configuration)
|
||||||
{
|
{
|
||||||
$instance = curry('is_a', 3)(_, _, true);
|
$supports = any(array_map(curry('is_a', 3)(_, _, true), [
|
||||||
|
StopRepository::class,
|
||||||
|
LineRepository::class,
|
||||||
|
DepartureRepository::class,
|
||||||
|
MessageRepository::class
|
||||||
|
]));
|
||||||
|
|
||||||
return any(
|
return $supports($configuration->getClass());
|
||||||
$instance(StopRepository::class),
|
|
||||||
$instance(LineRepository::class),
|
|
||||||
$instance(DepartureRepository::class)
|
|
||||||
)($configuration->getClass());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user