Skip to content

Commit

Permalink
Issue #45: unit tests for ImporterService and fixing minor API issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxist committed Jan 13, 2017
1 parent 875fc06 commit 21ecda1
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 39 deletions.
32 changes: 29 additions & 3 deletions src/Cantiga/Components/Hierarchy/PlaceLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* This file is part of Cantiga Project. Copyright 2016 Cantiga contributors.
* This file is part of Cantiga Project. Copyright 2017 Cantiga contributors.
*
* Cantiga Project is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -13,7 +13,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* along with Cantiga; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
declare(strict_types=1);
Expand All @@ -23,9 +23,35 @@
use Cantiga\Components\Hierarchy\HierarchicalInterface;
use Cantiga\Components\Hierarchy\User\CantigaUserRefInterface;

/**
* Generic interface for loading place entities of different types. Implementing this interface
* is mandatory to add a support for new types of places.
*/
interface PlaceLoaderInterface
{
/**
* Loads a place of the given type by its ID.
*
* @param int $id Place ID
* @return HierarchicalInterface Place entity
*/
public function loadPlaceById(int $id): HierarchicalInterface;
/**
* Loads the place, using the place reference information (used e.g. by the membership information).
*
* @param PlaceRef $place Reference to the place
* @return HierarchicalInterface Place entity
*/
public function loadPlace(PlaceRef $place): HierarchicalInterface;
public function loadPlaceForImport(HierarchicalInterface $currentPlace, CantigaUserRefInterface $member);
/**
* Cantiga projects may be a continuation of another archived project. In this case, it is possible to implement
* an import of settings from the corresponding archived place with the same name the user was member of. The method
* receives the current place as a reference point, and shall return the corresponding place in the archived parent
* project. The second argument is the current user.
*
* @param HierarchicalInterface $currentPlace Current place (import destination)
* @param CantigaUserRefInterface $member Current user
* @return ?HierarchicalInterface Source place for import, if exists
*/
public function loadPlaceForImport(HierarchicalInterface $currentPlace, CantigaUserRefInterface $member): ?HierarchicalInterface;
}
18 changes: 11 additions & 7 deletions src/Cantiga/CoreBundle/Repository/Place/AreaLoader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* This file is part of Cantiga Project. Copyright 2016 Cantiga contributors.
* This file is part of Cantiga Project. Copyright 2017 Cantiga contributors.
*
* Cantiga Project is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,12 +29,12 @@
class AreaLoader implements PlaceLoaderInterface
{
private $conn;

public function __construct(Connection $conn)
{
$this->conn = $conn;
}

public function loadPlace(PlaceRef $place): HierarchicalInterface
{
$area = Area::fetchByPlaceRef($this->conn, $place);
Expand All @@ -43,7 +43,7 @@ public function loadPlace(PlaceRef $place): HierarchicalInterface
}
return $area;
}

public function loadPlaceById(int $id): HierarchicalInterface
{
$area = Area::fetchActive($this->conn, $id);
Expand All @@ -52,9 +52,13 @@ public function loadPlaceById(int $id): HierarchicalInterface
}
return $area;
}
public function loadPlaceForImport(HierarchicalInterface $currentPlace, CantigaUserRefInterface $member)

public function loadPlaceForImport(HierarchicalInterface $currentPlace, CantigaUserRefInterface $member): ?HierarchicalInterface
{
return Area::fetchForImport($this->conn, $currentPlace, $member);
$area = Area::fetchForImport($this->conn, $currentPlace, $member);
if (false === $area) {
return null;
}
return $area;
}
}
20 changes: 12 additions & 8 deletions src/Cantiga/CoreBundle/Repository/Place/GroupLoader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* This file is part of Cantiga Project. Copyright 2016 Cantiga contributors.
* This file is part of Cantiga Project. Copyright 2017 Cantiga contributors.
*
* Cantiga Project is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,12 +29,12 @@
class GroupLoader implements PlaceLoaderInterface
{
private $conn;

public function __construct(Connection $conn)
{
$this->conn = $conn;
}

public function loadPlace(PlaceRef $place): HierarchicalInterface
{
$group = Group::fetchByPlaceRef($this->conn, $place);
Expand All @@ -43,7 +43,7 @@ public function loadPlace(PlaceRef $place): HierarchicalInterface
}
return $group;
}

public function loadPlaceById(int $id): HierarchicalInterface
{
$group = Group::fetch($this->conn, $id);
Expand All @@ -52,9 +52,13 @@ public function loadPlaceById(int $id): HierarchicalInterface
}
return $group;
}
public function loadPlaceForImport(HierarchicalInterface $currentPlace, CantigaUserRefInterface $member)

public function loadPlaceForImport(HierarchicalInterface $currentPlace, CantigaUserRefInterface $member): ?HierarchicalInterface
{
return Group::fetchForImport($this->conn, $currentPlace, $member);
$group = Group::fetchForImport($this->conn, $currentPlace, $member);
if (false === $group) {
return null;
}
return $group;
}
}
}
18 changes: 11 additions & 7 deletions src/Cantiga/CoreBundle/Repository/Place/ProjectLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
class ProjectLoader implements PlaceLoaderInterface
{
private $conn;

public function __construct(Connection $conn)
{
$this->conn = $conn;
}

public function loadPlace(PlaceRef $place): HierarchicalInterface
{
$project = Project::fetchByPlaceRef($this->conn, $place);
Expand All @@ -43,7 +43,7 @@ public function loadPlace(PlaceRef $place): HierarchicalInterface
}
return $project;
}

public function loadPlaceById(int $id): HierarchicalInterface
{
$project = Project::fetchActive($this->conn, $id);
Expand All @@ -52,9 +52,13 @@ public function loadPlaceById(int $id): HierarchicalInterface
}
return $project;
}
public function loadPlaceForImport(HierarchicalInterface $currentPlace, CantigaUserRefInterface $member)

public function loadPlaceForImport(HierarchicalInterface $currentPlace, CantigaUserRefInterface $member): ?HierarchicalInterface
{
return Project::fetchForImport($this->conn, $currentPlace, $member);
$project = Project::fetchForImport($this->conn, $currentPlace, $member);
if (false === $project) {
return null;
}
return $group;
}
}
}
32 changes: 18 additions & 14 deletions src/Cantiga/CoreBundle/Services/ImporterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ImporterService implements ImporterInterface
private $membershipStorage;
private $tokenStorage;
/**
* @var TranslatorInterface
* @var TranslatorInterface
*/
private $translator;
/**
Expand All @@ -45,24 +45,24 @@ class ImporterService implements ImporterInterface
* @var array
*/
private $cache;

public function __construct(MembershipStorageInterface $membershipStorage, TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
{
$this->membershipStorage = $membershipStorage;
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
}

public function addPlaceLoader(string $type, PlaceLoaderInterface $loader)
{
$this->loaders[$type] = $loader;
}

public function getImportLabel(): string
{
if ($this->membershipStorage->hasMembership()) {
$place = $this->loadMatchingPlace($this->membershipStorage->getMembership());
if (false !== $place) {
if (null !== $place) {
$project = $this->getProject($place);
return $this->translator->trans('Import from 0', [$project->getName()], 'general');
}
Expand All @@ -74,14 +74,18 @@ public function isImportAvailable(): bool
{
if ($this->membershipStorage->hasMembership()) {
$place = $this->loadMatchingPlace($this->membershipStorage->getMembership());
return false !== $place;
return null !== $place;
}
return false;
}

public function getImportSource(): HierarchicalInterface
{
return $this->loadMatchingPlace($this->membershipStorage->getMembership());
$place = $this->loadMatchingPlace($this->membershipStorage->getMembership());
if (null === $place) {
throw new \LogicException('There is no place to import anything from.');
}
return $place;
}

public function getImportDestination(): HierarchicalInterface
Expand All @@ -95,8 +99,8 @@ public function getImportQuestion(string $pageTitle, string $question): Question
$helper->title($pageTitle, $this->getImportLabel());
return $helper;
}
private function getProject(HierarchicalInterface $place): Project

private function getProject(HierarchicalInterface $place): HierarchicalInterface
{
if ($place->isRoot()) {
return $place;
Expand All @@ -112,12 +116,12 @@ private function loadMatchingPlace(Membership $membership)
if (isset($this->cache[$key])) {
return $this->cache[$key];
}

if (!isset($this->loaders[$place->getTypeName()])) {
throw new LogicException('Place loader not registered for place type \''.$place->getTypeName().'\'');
throw new \LogicException('Place loader not registered for place type \''.$place->getTypeName().'\'');
}

$loader = $this->loaders[$place->getTypeName()];
return $this->cache[$key] = $loader->loadPlaceForImport($membership->getPlace(), $this->tokenStorage->getToken()->getUser());
}
}
}
Loading

0 comments on commit 21ecda1

Please sign in to comment.