Skip to content

Commit da3cf67

Browse files
pawbujadamwojs
authored andcommitted
Added support for Zielone Tarasy (#1)
Added support for Zielone Tarasy
1 parent cdcf5e8 commit da3cf67

23 files changed

+529
-107
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# awokado.robot
22

3-
Slack bot providing Awokado Lunch Bar menu for today
3+
Slack bot providing menu of lunch bars and restaurants for today
44

55
![Screenshot](https://www.dropbox.com/s/kzrvihbnut3u1lv/Awokado%32Robot%32Preview.png?raw=1)
66

@@ -14,6 +14,10 @@ Slack bot providing Awokado Lunch Bar menu for today
1414
## Usage
1515

1616
```sh
17-
php awokado-robot <incoming-webhook-url>
17+
php awokado-robot <restaurant> <incoming-webhook-url>
1818
```
1919

20+
## Supported restaurants
21+
22+
1. [awokado](http://awokado.krakow.pl) - Awokado Lunch Bar ul. Prądnicka 12, 30-002 Kraków
23+
1. [zielone-tarasy](https://www.zielone-tarasy.eu) - Zielone Tarasy Restro Bar Gallery al. Słowackiego 64, 30-004 Kraków

awokado-robot

+21-30
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,32 @@
44
require __DIR__ . '/vendor/autoload.php';
55

66
use AdamWojs\AwokadoRobot\AwokadoRobot;
7-
use AdamWojs\AwokadoRobot\MenuProvider;
8-
use AdamWojs\AwokadoRobot\Notification\SlackWebhookTransport;
9-
use Goutte\Client;
10-
use GuzzleHttp\Client as GuzzleClient;
11-
use GuzzleHttp\HandlerStack;
12-
use GuzzleHttp\MessageFormatter;
13-
use GuzzleHttp\Middleware;
14-
use Monolog\Handler\StreamHandler;
15-
use Monolog\Logger;
16-
17-
if ($argc < 2) {
18-
exit("usage: $argv[0] <webhook-url>");
19-
}
20-
21-
$logger = new Logger('awokado-robot');
22-
$logger->pushHandler(new StreamHandler(__DIR__.'/var/logs/awokado.log', Logger::DEBUG));
7+
use AdamWojs\AwokadoRobot\Notification\SlackWebhookTransportFactory;
8+
use AdamWojs\AwokadoRobot\Menu\Provider\Awokado\AwokadoMenuProviderFactory;
9+
use AdamWojs\AwokadoRobot\Menu\Provider\ZieloneTarasy\ZieloneTarasyMenuProviderFactory;
10+
use AdamWojs\AwokadoRobot\LoggerFactory;
2311

24-
$stack = HandlerStack::create();
25-
$stack->push(Middleware::log($logger, new MessageFormatter('{request} - {response}')));
12+
$restaurants = [
13+
'awokado' => AwokadoMenuProviderFactory::class,
14+
'zielone-tarasy' => ZieloneTarasyMenuProviderFactory::class
15+
];
2616

27-
$goutteClient = new Client();
28-
$goutteClient->setClient(new GuzzleClient([
29-
'timeout' => 180,
30-
'handler' => $stack,
31-
]));
3217

33-
$menuProvider = new MenuProvider($goutteClient, 'http://awokado.krakow.pl/lunch-bar/menu/');
34-
$transport = new SlackWebhookTransport(new GuzzleClient([
35-
'handler' => $stack
36-
]), $argv[1]);
18+
if ($argc < 3) {
19+
exit("usage: $argv[0] <restaurant> <webhook-url>");
20+
}
3721

38-
$robot = new AwokadoRobot($menuProvider, $transport);
39-
$robot->setLogger($logger);
40-
$robot->run();
22+
$restaurant = $argv[1];
23+
$webhook = $argv[2];
4124

4225

26+
if(empty($restaurants[$restaurant])) {
27+
exit("unknown restaurant");
28+
}
4329

30+
$menuProvider = $restaurants[$restaurant]::create();
31+
$transport = SlackWebhookTransportFactory::create($webhook);
4432

33+
$robot = new AwokadoRobot($menuProvider, $transport);
34+
$robot->setLogger(LoggerFactory::get());
35+
$robot->run();

src/AwokadoRobot.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace AdamWojs\AwokadoRobot;
44

5+
use AdamWojs\AwokadoRobot\Menu\Provider\MenuProviderInterface;
6+
use DateTimeImmutable;
57
use Exception;
68
use Psr\Log\LoggerAwareTrait;
79
use Psr\Log\NullLogger;
@@ -32,15 +34,17 @@ public function __construct(MenuProviderInterface $menuProvider, Notification\Tr
3234
public function run(): void
3335
{
3436
try {
35-
$this->logger->info('Fetching menu...');
37+
$today = new DateTimeImmutable();
3638

37-
$menu = $this->menuProvider->getCurrentMenu();
38-
if (null === $menu) {
39+
if (!$this->menuProvider->isMenuAvailable($today)) {
3940
$this->logger->info('Menu is not available');
4041

4142
return;
4243
}
4344

45+
$this->logger->info('Fetching menu...');
46+
$menu = $this->menuProvider->getMenu($today);
47+
4448
$this->logger->info('Sending menu...');
4549
$this->transport->send($menu);
4650
$this->logger->info('Menu has been sent.');

src/LoggerFactory.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace AdamWojs\AwokadoRobot;
4+
5+
use Monolog\Handler\StreamHandler;
6+
use Monolog\Logger;
7+
8+
class LoggerFactory
9+
{
10+
/**
11+
* @var Logger
12+
*/
13+
private static $logger;
14+
15+
/**
16+
* @return Logger
17+
* @throws \Exception
18+
*/
19+
public static function get(): Logger
20+
{
21+
if (self::$logger) {
22+
return self::$logger;
23+
}
24+
25+
$logger = new Logger('awokado-robot');
26+
$logger->pushHandler(new StreamHandler(__DIR__ . '/../var/logs/awokado.log', Logger::DEBUG));
27+
self::$logger = $logger;
28+
29+
return self::$logger;
30+
}
31+
}

src/Menu.php

-36
This file was deleted.

src/Menu/Menu.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace AdamWojs\AwokadoRobot\Menu;
4+
5+
use DateTimeInterface;
6+
7+
class Menu
8+
{
9+
/** @var DateTimeInterface */
10+
private $date;
11+
12+
/** @var @var string */
13+
private $restaurant;
14+
15+
/** @var MenuItem[] */
16+
private $items;
17+
18+
/**
19+
* Menu constructor.
20+
*
21+
* @param string $restaurant
22+
* @param DateTimeInterface $date
23+
* @param MenuItem[] $items
24+
*/
25+
public function __construct(string $restaurant, DateTimeInterface $date, array $items)
26+
{
27+
$this->restaurant = $restaurant;
28+
$this->date = $date;
29+
$this->items = $items;
30+
}
31+
32+
/**
33+
* @return DateTimeInterface
34+
*/
35+
public function getDate(): DateTimeInterface
36+
{
37+
return $this->date;
38+
}
39+
40+
/**
41+
* @return MenuItem[]
42+
*/
43+
public function getItems(): array
44+
{
45+
return $this->items;
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getRestaurant(): string
52+
{
53+
return $this->restaurant;
54+
}
55+
}

src/MenuItem.php src/Menu/MenuItem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace AdamWojs\AwokadoRobot;
3+
namespace AdamWojs\AwokadoRobot\Menu;
44

55
class MenuItem
66
{

src/MenuProvider.php src/Menu/Provider/Awokado/AwokadoMenuProvider.php

+33-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3-
namespace AdamWojs\AwokadoRobot;
3+
namespace AdamWojs\AwokadoRobot\Menu\Provider\Awokado;
44

5-
use DateTimeImmutable;
5+
use AdamWojs\AwokadoRobot\Menu\Menu;
6+
use AdamWojs\AwokadoRobot\Menu\MenuItem;
7+
use AdamWojs\AwokadoRobot\Menu\Provider\MenuProviderInterface;
68
use DateTimeInterface;
79
use Goutte\Client;
810

9-
class MenuProvider implements MenuProviderInterface
11+
class AwokadoMenuProvider implements MenuProviderInterface
1012
{
1113
const AWOKADO_MENU_SELECTOR = '.tabcontent';
1214
const SEPARATOR = '';
@@ -32,20 +34,37 @@ public function __construct(Client $client, string $websiteUrl)
3234
/**
3335
* {@inheritdoc}
3436
*/
35-
public function getCurrentMenu(): ?Menu
37+
public function getRestaurant(): string
3638
{
37-
$today = new DateTimeImmutable();
39+
return 'Awokado Lunch Bar';
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function isMenuAvailable(DateTimeInterface $date): bool
46+
{
47+
return $date->format('N') <= 5;
48+
}
3849

39-
if (!$this->isMenuAvailable($today)) {
40-
// Menu is not available
41-
return null;
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function getMenu(DateTimeInterface $date): ?Menu
54+
{
55+
if (!$this->isMenuAvailable($date)) {
56+
throw new \RuntimeException('Menu not available');
4257
}
4358

44-
$items = $this->parseMenu($this->fetchMenu($today));
59+
$items = $this->parseMenu($this->fetchMenu($date));
4560

46-
return new Menu($today, $items);
61+
return new Menu($this->getRestaurant(), $date, $items);
4762
}
4863

64+
/**
65+
* @param DateTimeInterface $date
66+
* @return string
67+
*/
4968
private function fetchMenu(DateTimeInterface $date): string
5069
{
5170
return $this->client
@@ -55,6 +74,10 @@ private function fetchMenu(DateTimeInterface $date): string
5574
->text();
5675
}
5776

77+
/**
78+
* @param string $text
79+
* @return array
80+
*/
5881
private function parseMenu(string $text): array
5982
{
6083
$items = [];
@@ -72,9 +95,4 @@ private function parseMenu(string $text): array
7295

7396
return $items;
7497
}
75-
76-
private function isMenuAvailable(DateTimeInterface $date): bool
77-
{
78-
return $date->format('N') <= 5;
79-
}
8098
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace AdamWojs\AwokadoRobot\Menu\Provider\Awokado;
4+
5+
use AdamWojs\AwokadoRobot\Menu\Provider\MenuProviderFactoryInterface;
6+
use AdamWojs\AwokadoRobot\Menu\Provider\MenuProviderInterface;
7+
use AdamWojs\AwokadoRobot\StackFactory;
8+
use Goutte\Client;
9+
use GuzzleHttp\Client as GuzzleClient;
10+
11+
class AwokadoMenuProviderFactory implements MenuProviderFactoryInterface
12+
{
13+
/**
14+
* @return AwokadoMenuProvider
15+
* @throws \Exception
16+
*/
17+
public static function create(): MenuProviderInterface
18+
{
19+
$stack = StackFactory::create();
20+
21+
$goutteClient = new Client();
22+
$goutteClient->setClient(new GuzzleClient([
23+
'timeout' => 180,
24+
'handler' => $stack,
25+
]));
26+
27+
return new AwokadoMenuProvider($goutteClient, 'http://awokado.krakow.pl/lunch-bar/menu/');
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace AdamWojs\AwokadoRobot\Menu\Provider;
4+
5+
interface MenuProviderFactoryInterface
6+
{
7+
/**
8+
* @return MenuProviderInterface
9+
*/
10+
public static function create(): MenuProviderInterface;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace AdamWojs\AwokadoRobot\Menu\Provider;
4+
5+
use AdamWojs\AwokadoRobot\Menu\Menu;
6+
use DateTimeInterface;
7+
8+
interface MenuProviderInterface
9+
{
10+
/**
11+
* @param \DateTimeInterface $date
12+
* @return Menu|null
13+
*/
14+
public function getMenu(DateTimeInterface $date): ?Menu;
15+
16+
/**
17+
* @param \DateTimeInterface $date
18+
* @return bool
19+
*/
20+
public function isMenuAvailable(DateTimeInterface $date): bool;
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getRestaurant(): string;
26+
}

0 commit comments

Comments
 (0)