-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from JarekW/refactor
refactoring of translatable listener
- Loading branch information
Showing
8 changed files
with
506 additions
and
337 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
lib/FSi/DoctrineExtensions/Translatable/ClassTranslationContext.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,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; | ||
} | ||
} |
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
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
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
59 changes: 59 additions & 0 deletions
59
lib/FSi/DoctrineExtensions/Translatable/Mapping/TranslationAssociationMetadata.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,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; | ||
} | ||
} |
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
Oops, something went wrong.