Remove VocabularyController

This commit is contained in:
Benoît Burnichon
2016-03-30 16:33:10 +02:00
parent 86cba70a76
commit 020d7a9b02
6 changed files with 97 additions and 177 deletions

View File

@@ -12,8 +12,8 @@ namespace Alchemy\Phrasea\Controller\Admin;
use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Metadata\TagProvider;
use Alchemy\Phrasea\Vocabulary\Controller as VocabularyController;
use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface;
use Assert\Assertion;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -96,24 +96,57 @@ class FieldsController extends Controller
public function listVocabularies()
{
$vocabularies = VocabularyController::getAvailable($this->app);
return $this->app->json(array_map(
[$this, 'getVocabularyAsArray'],
$this->fetchVocabularies()
));
}
return $this->app->json(array_map(function (ControlProviderInterface $vocabulary) {
/**
* @return ControlProviderInterface[]
*/
private function fetchVocabularies()
{
$vocabularies = $this->getVocabularies();
$instances = array_map(
function ($type) use ($vocabularies) {
return $vocabularies[$type];
},
$vocabularies->keys()
);
Assertion::allIsInstanceOf($instances, ControlProviderInterface::class);
return $instances;
}
/**
* @param string $type
* @return ControlProviderInterface
*/
private function fetchVocabulary($type)
{
$vocabularies = $this->getVocabularies();
$vocabulary = $vocabularies[$type];
Assertion::isInstanceOf($vocabulary, ControlProviderInterface::class);
return $vocabulary;
}
private function getVocabularyAsArray(ControlProviderInterface $vocabulary)
{
return [
'type' => $vocabulary->getType(),
'name' => $vocabulary->getName(),
];
}, $vocabularies));
}
public function getVocabulary($type)
{
$vocabulary = VocabularyController::get($this->app, $type);
return $this->app->json([
'type' => $vocabulary->getType(),
'name' => $vocabulary->getName(),
]);
return $this->app->json($this->getVocabularyAsArray($this->fetchVocabulary($type)));
}
public function searchTag(Request $request)
@@ -293,11 +326,11 @@ class FieldsController extends Controller
}
try {
$vocabulary = VocabularyController::get($this->app, $data['vocabulary-type']);
$vocabulary = $this->fetchVocabulary($data['vocabulary-type']);
$field->setVocabularyControl($vocabulary);
$field->setVocabularyRestricted($data['vocabulary-restricted']);
} catch (\InvalidArgumentException $e) {
// Invalid vocabulary requested
}
if ('' !== $dcesElement = (string) $data['dces-element']) {
@@ -347,4 +380,12 @@ class FieldsController extends Controller
return $data;
}
/**
* @return ControlProviderInterface[]|\Pimple
*/
private function getVocabularies()
{
return $this->app['vocabularies'];
}
}

View File

@@ -20,7 +20,6 @@ use Alchemy\Phrasea\Model\Entities\Preset;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\Manipulator\PresetManipulator;
use Alchemy\Phrasea\Model\Repositories\PresetRepository;
use Alchemy\Phrasea\Vocabulary\Controller as VocabularyController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -240,7 +239,7 @@ class EditController extends Controller
throw new \Exception('Invalid sbas_id');
}
$VC = VocabularyController::get($this->app, $vocabulary);
$vocabularyProvider = $this->app['vocabularies'][$vocabulary];
$databox = $this->findDataboxById($sbas_id);
} catch (\Exception $e) {
$data['message'] = $this->app->trans('Vocabulary not found');
@@ -250,7 +249,7 @@ class EditController extends Controller
$query = $request->query->get('query');
$results = $VC->find($query, $this->getAuthenticatedUser(), $databox);
$results = $vocabularyProvider->find($query, $this->getAuthenticatedUser(), $databox);
$list = [];

View File

@@ -1,51 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Vocabulary;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface;
class Controller
{
/**
* Factory of ControlProvider
*
* @param Application $app
* @param string $type
*
* @return ControlProviderInterface
*
* @throws \InvalidArgumentException
*/
public static function get(Application $app, $type)
{
$classname = __NAMESPACE__ . '\\ControlProvider\\' . $type . 'Provider';
if ( ! class_exists($classname)) {
throw new \InvalidArgumentException('Vocabulary type not found');
}
return new $classname($app);
}
/**
* Returns an array of available ControlProviders
*
* @return array
*/
public static function getAvailable(Application $app)
{
return [
new ControlProvider\UserProvider($app)
];
}
}

View File

@@ -1,5 +1,4 @@
<?php
/*
* This file is part of Phraseanet
*
@@ -15,7 +14,6 @@ use Alchemy\Phrasea\Core\Event\Record\Structure\FieldEvent;
use Alchemy\Phrasea\Core\Event\Record\Structure\FieldUpdatedEvent;
use Alchemy\Phrasea\Core\Event\Record\Structure\RecordStructureEvents;
use Alchemy\Phrasea\Metadata\TagFactory;
use Alchemy\Phrasea\Vocabulary;
use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface;
use Alchemy\Phrasea\Metadata\Tag\NoSource;
use Doctrine\DBAL\Connection;
@@ -1012,7 +1010,7 @@ class databox_field implements cache_cacheableInterface
}
try {
$this->Vocabulary = Vocabulary\Controller::get($this->app, $this->VocabularyType);
$this->Vocabulary = $this->app['vocabularies'][$this->VocabularyType];
} catch (\InvalidArgumentException $e) {
// Could not find Vocabulary
}

View File

@@ -3,8 +3,6 @@
namespace Alchemy\Tests\Phrasea\Controller\Admin;
use PHPExiftool\Driver\Tag\IPTC\ObjectName;
use Alchemy\Phrasea\Vocabulary\Controller as VocabularyController;
use Symfony\Component\HttpKernel\Client;
/**
* @group functional
@@ -16,18 +14,17 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
{
public function testRoot()
{
$databoxes = self::$DI['app']->getDataboxes();
$databoxes = $this->getApplication()->getDataboxes();
$databox = array_shift($databoxes);
self::$DI['client']->request("GET", "/admin/fields/" . $databox->get_sbas_id());
$response = $this->request("GET", "/admin/fields/" . $databox->get_sbas_id());
$this->assertTrue(self::$DI['client']->getResponse()->isOk());
$this->assertTrue($response->isOk());
}
public function testLanguage()
{
self::$DI['client']->request("GET", "/admin/fields/language.json");
$response = self::$DI['client']->getResponse();
$response = $this->request("GET", "/admin/fields/language.json");
$this->assertTrue($response->isOk());
$this->assertEquals("application/json", $response->headers->get("content-type"));
@@ -37,9 +34,7 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
{
$tag = new ObjectName();
self::$DI['client']->request("GET", "/admin/fields/tags/".$tag->getTagname());
$response = self::$DI['client']->getResponse();
$response = $this->request("GET", "/admin/fields/tags/".$tag->getTagname());
$this->assertEquals("application/json", $response->headers->get("content-type"));
$data = json_decode($response->getContent(), true);
@@ -52,13 +47,11 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
public function testListDcFields()
{
self::$DI['client']->request("GET", "/admin/fields/dc-fields");
$response = $this->request("GET", "/admin/fields/dc-fields");
$response = self::$DI['client']->getResponse()->getContent();
$this->assertEquals("application/json", $response->headers->get("content-type"));
$this->assertEquals("application/json", self::$DI['client']->getResponse()->headers->get("content-type"));
$data = json_decode($response, true);
$data = json_decode($response->getContent(), true);
$this->assertInternalType('array', $data);
foreach ($data as $dc) {
@@ -72,49 +65,40 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
public function testListVocabularies()
{
self::$DI['client']->request("GET", "/admin/fields/vocabularies");
$response = $this->request("GET", "/admin/fields/vocabularies");
$response = self::$DI['client']->getResponse()->getContent();
$this->assertEquals("application/json", self::$DI['client']->getResponse()->headers->get("content-type"));
$this->assertEquals("application/json", $response->headers->get("content-type"));
$data = json_decode($response, true);
$data = json_decode($response->getContent(), true);
$this->assertInternalType('array', $data);
foreach ($data as $vocabulary) {
$this->assertArrayHasKey('type', $vocabulary);
$this->assertArrayHasKey('name', $vocabulary);
$voc = VocabularyController::get(self::$DI['app'], $vocabulary['type']);
$this->assertInstanceOf('Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface', $voc);
}
}
public function testGetVocabulary()
{
self::$DI['client']->request("GET", "/admin/fields/vocabularies/user");
$response = $this->request("GET", "/admin/fields/vocabularies/user");
$response = self::$DI['client']->getResponse()->getContent();
$this->assertEquals("application/json", self::$DI['client']->getResponse()->headers->get("content-type"));
$this->assertEquals("application/json", $response->headers->get("content-type"));
$data = json_decode($response, true);
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('type', $data);
$this->assertEquals('User', $data['type']);
$this->assertArrayHasKey('name', $data);
$voc = VocabularyController::get(self::$DI['app'], $data['type']);
$this->assertInstanceOf('Alchemy\Phrasea\Vocabulary\ControlProvider\UserProvider', $voc);
}
public function testSearchTag()
{
self::$DI['client']->request("GET", "/admin/fields/tags/search?term=xmp-exif");
$response = $this->request("GET", "/admin/fields/tags/search?term=xmp-exif");
$response = self::$DI['client']->getResponse()->getContent();
$this->assertEquals("application/json", self::$DI['client']->getResponse()->headers->get("content-type"));
$this->assertEquals("application/json", $response->headers->get("content-type"));
$data = json_decode($response, true);
$data = json_decode($response->getContent(), true);
$this->assertGreaterThan(90, count($data));
@@ -128,7 +112,7 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
public function testUpdateFields()
{
$databoxes = self::$DI['app']->getDataboxes();
$databoxes = $this->getApplication()->getDataboxes();
$databox = array_shift($databoxes);
$fieldObjects = [];
// create two fields
@@ -170,7 +154,7 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
]];
foreach ($fields as $fieldData) {
$field = \databox_field::create(self::$DI['app'], $databox, $fieldData['name'], $fieldData['multi']);
$field = \databox_field::create($this->getApplication(), $databox, $fieldData['name'], $fieldData['multi']);
$field
->set_thumbtitle($fieldData['thumbtitle'])
->set_tag(\databox_field::loadClassFromTagName($fieldData['tag']))
@@ -197,13 +181,11 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$body[count($body) - 1]['readonly'] = true;
$body[count($body) - 1]['required'] = false;
self::$DI['client']->request("PUT", sprintf("/admin/fields/%d/fields", $databox->get_sbas_id()), [], [], [], json_encode($body));
$response = $this->request("PUT", sprintf("/admin/fields/%d/fields", $databox->get_sbas_id()), [], [], [], json_encode($body));
$response = self::$DI['client']->getResponse()->getContent();
$this->assertEquals("application/json", $response->headers->get("content-type"));
$this->assertEquals("application/json", self::$DI['client']->getResponse()->headers->get("content-type"));
$data = json_decode($response, true);
$data = json_decode($response->getContent(), true);
$this->assertTrue(is_array($data));
@@ -248,14 +230,11 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
'vocabulary-restricted' => true,
]);
/** @var Client $client */
$client = self::$DI['client'];
$client->request("POST", sprintf("/admin/fields/%d/fields", $databox->get_sbas_id()), [], [], [], $body);
$response = $this->request("POST", sprintf("/admin/fields/%d/fields", $databox->get_sbas_id()), [], [], [], $body);
$response = $client->getResponse()->getContent();
$this->assertEquals("application/json", $client->getResponse()->headers->get("content-type"));
$this->assertEquals("application/json", $response->headers->get("content-type"));
$data = json_decode($response, true);
$data = json_decode($response->getContent(), true);
$this->assertTrue(is_array($data));
@@ -271,15 +250,14 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
public function testListField()
{
$databoxes = self::$DI['app']->getDataboxes();
$databoxes = $this->getApplication()->getDataboxes();
$databox = array_shift($databoxes);
self::$DI['client']->request("GET", sprintf("/admin/fields/%d/fields", $databox->get_sbas_id()));
$response = $this->request("GET", sprintf("/admin/fields/%d/fields", $databox->get_sbas_id()));
$response = self::$DI['client']->getResponse()->getContent();
$this->assertEquals("application/json", self::$DI['client']->getResponse()->headers->get("content-type"));
$this->assertEquals("application/json", $response->headers->get("content-type"));
$data = json_decode($response, true);
$data = json_decode($response->getContent(), true);
$this->assertInternalType('array', $data);
@@ -297,13 +275,11 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$data = $field->toArray();
$client = $this->getClient();
$client->request("GET", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()));
$response = $this->request("GET", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()));
$response = $client->getResponse()->getContent();
$this->assertEquals("application/json", $client->getResponse()->headers->get("content-type"));
$this->assertEquals("application/json", $response->headers->get("content-type"));
$this->assertEquals($data, json_decode($response, true));
$this->assertEquals($data, json_decode($response->getContent(), true));
$field->delete();
}
@@ -320,11 +296,9 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$data['business'] = true;
$data['vocabulary-type'] = 'User';
$client = $this->getClient();
$client->request("PUT", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()), [], [], [], json_encode($data));
$response = $this->request("PUT", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()), [], [], json_encode($data));
$response = $client->getResponse()->getContent();
$this->assertEquals($data, json_decode($response, true));
$this->assertEquals($data, json_decode($response->getContent(), true));
$field->delete();
}
@@ -342,13 +316,10 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$data['business'] = true;
$data['vocabulary-type'] = 'User';
/** @var Client $client */
$client = self::$DI['client'];
$client->request("DELETE", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()), [], [], [], json_encode($data));
$response = $this->request("DELETE", sprintf("/admin/fields/%d/fields/%d", $databox->get_sbas_id(), $field->get_id()), [], [], json_encode($data));
$response = $client->getResponse()->getContent();
$this->assertEquals('', $response);
$this->assertEquals(204, $client->getResponse()->getStatusCode());
$this->assertEquals('', $response->getContent());
$this->assertEquals(204, $response->getStatusCode());
try {
$databox->get_meta_structure()->get_element($fieldId);

View File

@@ -1,38 +0,0 @@
<?php
namespace Alchemy\Tests\Phrasea\Vocabulary;
use Alchemy\Phrasea\Vocabulary\Controller;
/**
* @group functional
* @group legacy
*/
class ControllerTest extends \PhraseanetTestCase
{
public function testGet()
{
$provider = Controller::get(self::$DI['app'], 'User');
$this->assertInstanceOf('\\Alchemy\\Phrasea\\Vocabulary\\ControlProvider\\UserProvider', $provider);
try {
$provider = Controller::get(self::$DI['app'], 'Zebulon');
$this->fail('Should raise an exception');
} catch (\Exception $e) {
}
}
public function testGetAvailable()
{
$available = Controller::getAvailable(self::$DI['app']);
$this->assertTrue(is_array($available));
foreach ($available as $controller) {
$this->assertInstanceOf('\\Alchemy\\Phrasea\\Vocabulary\\ControlProvider\\ControlProviderInterface', $controller);
}
}
}