Add Schedule bundle

This commit is contained in:
Oliver Davies 2019-05-23 01:05:29 +01:00
parent 9ed1314762
commit 52f909da6d
7 changed files with 127 additions and 1 deletions

View file

@ -1,5 +1,6 @@
<?php
use App\Schedule\SculpinScheduleBundle;
use App\Speakers\SculpinSpeakersBundle;
use Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel;
@ -8,6 +9,7 @@ class SculpinKernel extends AbstractKernel
protected function getAdditionalSculpinBundles(): array
{
return [
SculpinScheduleBundle::class,
SculpinSpeakersBundle::class,
];
}

View file

@ -33,7 +33,8 @@
},
"autoload": {
"psr-4": {
"App\\Speakers\\": "src/Speakers/src"
"App\\Speakers\\": "src/Speakers/src",
"App\\Schedule\\": "src/Schedule/src"
}
},
"autoload-dev": {

View file

@ -0,0 +1,17 @@
<?php
namespace App\Schedule\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class SculpinScheduleExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../'));
$loader->load('services.yml');
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace App\Schedule\Model;
use Tightenco\Collect\Support\Collection;
class Session
{
private $data;
public function __construct($data)
{
$this->data = $data;
}
public function getSlot(): int
{
return (int) $this->data['slot'];
}
public function getTitle(): string
{
return $this->data['title'];
}
public function getSpeakers(): Collection
{
return collect($this->data['speakers']);
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace App\Schedule;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class SculpinScheduleBundle extends Bundle
{
}

View file

@ -0,0 +1,29 @@
<?php
namespace App\Schedule\TwigExtension;
use App\Schedule\Model\Session;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class ScheduleExtension extends AbstractExtension
{
/**
* {@inheritDoc}
*/
public function getFunctions()
{
return [
new TwigFunction('sessionInSlot', [$this, 'getSessionInSlot']),
];
}
public function getSessionInSlot(int $slotId, array $sessions): ?Session
{
return collect($sessions)->map(function ($session): ?Session {
return new Session($session);
})->first(function (?Session $session) use ($slotId): bool {
return $session->getSlot() == $slotId;
});
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace App\Tests\Schedule;
use App\Schedule\Model\Session;
use App\Schedule\TwigExtension\ScheduleExtension;
use PHPUnit\Framework\TestCase;
class ScheduleTest extends TestCase
{
/**
* @var \App\Schedule\TwigExtension\ScheduleExtension
*/
private $extension;
protected function setUp(): void
{
parent::setUp();
$this->extension = new ScheduleExtension();
}
/** @test */
public function get_a_session_for_a_slot()
{
$sessions = [
['title' => 'The Real State of Drupal', 'slot' => 4],
['title' => 'Using Ansible for automation', 'slot' => 1],
['title' => 'Introduction to Views', 'slot' => 3],
['title' => 'Doing Good with Drupal', 'slot' => 2],
];
$session = $this->extension->getSessionInSlot(3, $sessions);
$this->assertInstanceOf(Session::class, $session);
$this->assertSame('Introduction to Views', $session->getTitle());
}
}