-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f55425
commit 1ca2843
Showing
15 changed files
with
850 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
DependencyInjection/HandcraftedInTheAlpsSuluResourceExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
ListRepresentation/DoctrineListRepresentationFactoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.