Skip to content

Commit

Permalink
initial bundle (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored Feb 27, 2020
1 parent 3f55425 commit 1ca2843
Show file tree
Hide file tree
Showing 15 changed files with 850 additions and 21 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: PHP

on: pull_request

jobs:
tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- name: Validate composer.json and composer.lock
run: composer validate

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Linting code
run: composer lint

- name: Run unit tests
run: composer phpunit
22 changes: 22 additions & 0 deletions DependencyInjection/HandcraftedInTheAlpsSuluResourceExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace HandcraftedInTheAlps\Bundle\SuluResourceBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

class HandcraftedInTheAlpsSuluResourceExtension extends Extension
{
/**
* @param mixed[] $configs
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
}
}
25 changes: 25 additions & 0 deletions Exception/MissingResultException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace HandcraftedInTheAlps\Bundle\SuluResourceBundle\Exception;

class MissingResultException extends \Exception
{
/**
* @var string
*/
private $method;

public function __construct(string $method)
{
parent::__construct(sprintf('Result is missing for method "%s"', $method));

$this->method = $method;
}

public function getMethod(): string
{
return $this->method;
}
}
53 changes: 53 additions & 0 deletions Exception/ModelNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace HandcraftedInTheAlps\Bundle\SuluResourceBundle\Exception;

class ModelNotFoundException extends \Exception
{
/**
* @var string
*/
private $entity;

/**
* @var mixed[]
*/
private $criteria;

/**
* @param mixed[] $criteria
*/
public function __construct(string $entity, array $criteria, int $code = 0, \Throwable $previous = null)
{
$criteriaMessages = [];
foreach ($criteria as $key => $value) {
$criteriaMessages[] = sprintf('with %s "%s"', $key, $value);
}

$message = sprintf(
'Entity "%s" with %s not found',
$entity,
implode(' and ', $criteriaMessages)
);

parent::__construct($message, $code, $previous);

$this->entity = $entity;
$this->criteria = $criteria;
}

public function getEntity(): string
{
return $this->entity;
}

/**
* @return mixed[]
*/
public function getCriteria(): array
{
return $this->criteria;
}
}
92 changes: 92 additions & 0 deletions ListRepresentation/DoctrineListRepresentationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace HandcraftedInTheAlps\Bundle\SuluResourceBundle\ListRepresentation;

use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactory;
use Sulu\Component\Rest\ListBuilder\Doctrine\FieldDescriptor\DoctrineFieldDescriptor;
use Sulu\Component\Rest\ListBuilder\ListRestHelperInterface;
use Sulu\Component\Rest\ListBuilder\Metadata\FieldDescriptorFactoryInterface;
use Sulu\Component\Rest\ListBuilder\PaginatedRepresentation;
use Sulu\Component\Rest\RestHelperInterface;

class DoctrineListRepresentationFactory implements DoctrineListRepresentationFactoryInterface
{
/**
* @var RestHelperInterface
*/
private $restHelper;

/**
* @var ListRestHelperInterface
*/
private $listRestHelper;

/**
* @var DoctrineListBuilderFactory
*/
private $listBuilderFactory;

/**
* @var FieldDescriptorFactoryInterface
*/
private $fieldDescriptorFactory;

public function __construct(
RestHelperInterface $restHelper,
ListRestHelperInterface $listRestHelper,
DoctrineListBuilderFactory $listBuilderFactory,
FieldDescriptorFactoryInterface $fieldDescriptorFactory
) {
$this->restHelper = $restHelper;
$this->listRestHelper = $listRestHelper;
$this->listBuilderFactory = $listBuilderFactory;
$this->fieldDescriptorFactory = $fieldDescriptorFactory;
}

/**
* @param mixed[] $filters
* @param mixed[] $parameters
*/
public function createDoctrineListRepresentation(
string $resourceKey,
array $filters = [],
array $parameters = []
): PaginatedRepresentation {
/** @var DoctrineFieldDescriptor[] $fieldDescriptors */
$fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors($resourceKey);

$listBuilder = $this->listBuilderFactory->create($fieldDescriptors['id']->getEntityName());
$this->restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);

foreach ($parameters as $key => $value) {
$listBuilder->setParameter($key, $value);
}

foreach ($filters as $key => $value) {
$listBuilder->where($fieldDescriptors[$key], $value);
}

$items = $listBuilder->execute();

// sort the items to reflect the order of the given ids if the list was requested to include specific ids
$requestedIds = $this->listRestHelper->getIds();
if (null !== $requestedIds) {
usort($items, static function ($item1, $item2) use ($requestedIds) {
$item1Position = array_search($item1['id'], $requestedIds, true);
$item2Position = array_search($item2['id'], $requestedIds, true);

return $item1Position - $item2Position;
});
}

return new PaginatedRepresentation(
$items,
$resourceKey,
(int) $listBuilder->getCurrentPage(),
(int) $listBuilder->getLimit(),
(int) $listBuilder->count()
);
}
}
20 changes: 20 additions & 0 deletions ListRepresentation/DoctrineListRepresentationFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace HandcraftedInTheAlps\Bundle\SuluResourceBundle\ListRepresentation;

use Sulu\Component\Rest\ListBuilder\PaginatedRepresentation;

interface DoctrineListRepresentationFactoryInterface
{
/**
* @param mixed[] $filters
* @param mixed[] $parameters
*/
public function createDoctrineListRepresentation(
string $resourceKey,
array $filters = [],
array $parameters = []
): PaginatedRepresentation;
}
Loading

0 comments on commit 1ca2843

Please sign in to comment.