Skip to content

Commit

Permalink
Merge pull request #64 from JarekW/refactor
Browse files Browse the repository at this point in the history
refactoring of translatable listener
  • Loading branch information
rn0 committed Jan 12, 2016
2 parents 43515f6 + f4b9fd6 commit ef2cd90
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 337 deletions.
107 changes: 107 additions & 0 deletions lib/FSi/DoctrineExtensions/Translatable/ClassTranslationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* (c) FSi sp. z o.o. <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FSi\DoctrineExtensions\Translatable;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use FSi\DoctrineExtensions\Translatable\Mapping\TranslationAssociationMetadata;
use FSi\DoctrineExtensions\Translatable\Model\TranslatableRepositoryInterface;

/**
* @internal
*/
class ClassTranslationContext
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var ClassMetadata
*/
private $classMetadata;

/**
* @var TranslationAssociationMetadata
*/
private $associationMetadata;

public function __construct(
ObjectManager $objectManager,
ClassMetadata $classMetadata,
TranslationAssociationMetadata $associationMetadata
) {
$this->objectManager = $objectManager;
$this->classMetadata = $classMetadata;
$this->associationMetadata = $associationMetadata;
}

/**
* @return ClassMetadata
*/
public function getClassMetadata()
{
return $this->classMetadata;
}

/**
* @return Mapping\ClassMetadata
*/
public function getTranslatableMetadata()
{
return $this->associationMetadata->getClassMetadata();
}

/**
* @return TranslationAssociationMetadata
*/
public function getAssociationMetadata()
{
return $this->associationMetadata;
}

/**
* @return ClassMetadata
*/
public function getTranslationMetadata()
{
$associationName = $this->associationMetadata->getAssociationName();
$translationClass = $this->classMetadata->getAssociationTargetClass($associationName);
return $this->objectManager->getClassMetadata($translationClass);
}

/**
* @return TranslatableRepositoryInterface
* @throws Exception\AnnotationException
*/
public function getTranslatableRepository()
{
$repository = $this->objectManager->getRepository($this->classMetadata->getName());

if (!($repository instanceof TranslatableRepositoryInterface)) {
throw new Exception\AnnotationException(sprintf(
'Entity "%s" has "%s" as its "repositoryClass" which does not implement \FSi\DoctrineExtensions\Translatable\Model\TranslatableRepositoryInterface',
$this->classMetadata->getName(),
get_class($repository)
));
}

return $repository;
}

/**
* @return ObjectManager
*/
public function getObjectManager()
{
return $this->objectManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@ public function findTranslation($object, $locale, $translationAssociation = 'tra
}
}

/**
* @param object $object
* @param string $translationAssociation
* @return \Doctrine\Common\Collections\Collection
* @throws \FSi\DoctrineExtensions\Translatable\Exception\RuntimeException
*/
public function getTranslations($object, $translationAssociation = 'translations')
{
$translations = $this->getClassMetadata()->getFieldValue($object, $translationAssociation);

if ($translations === null) {
return new ArrayCollection();
}

if (!($translations instanceof Collection)) {
throw new RuntimeException(sprintf(
'Entity %s must contains implementation of "Doctrine\Common\Collections\Collection" in "%s" association',
$this->getClassName(),
$translationAssociation
));
}

return $translations;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -324,31 +349,6 @@ protected function areTranslationsIndexedByLocale($translationAssociation)
return ($translationAssociationMapping['indexBy'] == $translationExtendedMeta->localeProperty);
}

/**
* @param object $object
* @param string $translationAssociation
* @return \Doctrine\Common\Collections\Collection
* @throws \FSi\DoctrineExtensions\Translatable\Exception\RuntimeException
*/
protected function getTranslations($object, $translationAssociation)
{
$translations = $this->getClassMetadata()->getFieldValue($object, $translationAssociation);

if ($translations === null) {
return new ArrayCollection();
}

if (!($translations instanceof Collection)) {
throw new RuntimeException(sprintf(
'Entity %s must contains implementation of "Doctrine\Common\Collections\Collection" in "%s" association',
$this->getClassName(),
$translationAssociation
));
}

return $translations;
}

/**
* @param object $translation
* @param string $translationAssociation
Expand Down
15 changes: 15 additions & 0 deletions lib/FSi/DoctrineExtensions/Translatable/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace FSi\DoctrineExtensions\Translatable\Mapping;

use FSi\Component\Metadata\AbstractClassMetadata;
use Doctrine\Common\Persistence\Mapping\ClassMetadata as DoctrineClassMetadata;

class ClassMetadata extends AbstractClassMetadata
{
Expand Down Expand Up @@ -61,4 +62,18 @@ public function getTranslatableProperties()
{
return $this->translatableProperties;
}

/**
* @return TranslationAssociationMetadata[]
*/
public function getTranslationAssociationMetadatas()
{
$metadatas = array();

foreach ($this->getTranslatableProperties() as $association => $properties) {
$metadatas[] = new TranslationAssociationMetadata($this, $association, $properties);
}

return $metadatas;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Annotation extends AbstractAnnotationDriver
protected function loadExtendedClassMetadata(ClassMetadata $baseClassMetadata, ClassMetadataInterface $extendedClassMetadata)
{
$classReflection = $extendedClassMetadata->getClassReflection();

foreach ($classReflection->getProperties() as $property) {
if ($baseClassMetadata->isMappedSuperclass && !$property->isPrivate() ||
$baseClassMetadata->isInheritedField($property->name) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* (c) FSi sp. z o.o. <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FSi\DoctrineExtensions\Translatable\Mapping;

class TranslationAssociationMetadata
{
/**
* @var ClassMetadata
*/
private $classMetadata;

/**
* @var string
*/
private $association;

/**
* @var array
*/
private $properties;

public function __construct(ClassMetadata $classMetadata, $association, $properties)
{
$this->classMetadata = $classMetadata;
$this->association = $association;
$this->properties = $properties;
}

/**
* @return ClassMetadata
*/
public function getClassMetadata()
{
return $this->classMetadata;
}

/**
* @return string
*/
public function getAssociationName()
{
return $this->association;
}

/**
* @return array
*/
public function getProperties()
{
return $this->properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public function hasTranslation($object, $locale, $translationAssociation = 'tran
*/
public function getTranslation($object, $locale, $translationAssociation = 'translations');

/**
* @param $object
* @param string $translationAssociation
* @return \Doctrine\Common\Collections\Collection
* @throws \FSi\DoctrineExtensions\Translatable\Exception\RuntimeException
*/
public function getTranslations($object, $translationAssociation = 'translations');

/**
* @param object $object
* @param mixed $locale
Expand Down
Loading

0 comments on commit ef2cd90

Please sign in to comment.