Commit 1d7fff13 authored by e0ipso's avatar e0ipso
Browse files

fix: ensure Drupal coding standards

parent 1f32d12e
......@@ -2,10 +2,8 @@
namespace Drupal\typed_entity_example\TypedRepositories;
use Drupal\Component\Assertion\Inspector;
use Drupal\Core\Entity\Query\ConditionInterface;
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryBase;
use Drupal\typed_entity_example\WrappedEntities\Article;
/**
* The repository for articles.
......@@ -18,7 +16,10 @@ final class ArticleRepository extends TypedEntityRepositoryBase {
const FIELD_TAGS_NAME = 'field_tags';
/**
* Finds article by tags.
*
* @param string[] $tags
* The tags to search for.
*
* @return \Drupal\typed_entity_example\WrappedEntities\Article[]
* The wrapped entities.
......@@ -50,8 +51,8 @@ final class ArticleRepository extends TypedEntityRepositoryBase {
$field_path = static::FIELD_TAGS_NAME . '.entity.name';
$orGroup = array_reduce(
$tags,
function (ConditionInterface $or, string $tag) use ($field_path) {
return $or->condition($field_path, $tag, 'LIKE');
function (ConditionInterface $orCondition, string $tag) use ($field_path) {
return $orCondition->condition($field_path, $tag, 'LIKE');
},
$query->orConditionGroup()
);
......
......@@ -23,6 +23,8 @@ final class Article extends WrappedEntityBase {
/**
* Article constructor.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to wrap.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
......
......@@ -9,7 +9,13 @@ use Drupal\typed_entity\WrappedEntities\WrappedEntityBase;
*/
final class User extends WrappedEntityBase {
public function nickname() {
/**
* Get the user's nickname.
*
* @return string
* The nickname.
*/
public function nickname(): string {
// According to our stakeholders the nickname is the part before the @ in
// the registration email.
$email = $this->getEntity()->mail->value;
......
<?php
namespace Drupal\typed_entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
/**
* Wraps entities.
*/
interface EntityWrapperInterface {
/**
* Wraps an entity with business logic.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to wrap.
*
* @return \Drupal\typed_entity\WrappedEntities\WrappedEntityInterface
* The wrapped entity.
*
* @throws \Drupal\typed_entity\InvalidValueException
*/
public function wrap(EntityInterface $entity): WrappedEntityInterface;
/**
* Wraps an entities with business logic.
*
* @param \Drupal\Core\Entity\EntityInterface[] $entities
* The entity to wrap.
*
* @return \Drupal\typed_entity\WrappedEntities\WrappedEntityInterface[]
* The wrapped entities.
*
* @throws \Drupal\typed_entity\InvalidValueException
*/
public function wrapMultiple(array $entities): array;
}
......@@ -4,4 +4,7 @@ namespace Drupal\typed_entity;
use Exception;
/**
* Custom InvalidValueException.
*/
class InvalidValueException extends Exception {}
......@@ -8,7 +8,10 @@ use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryBase;
use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryInterface;
final class RepositoryCollector {
/**
* Collects the repositories and negotiates from a loaded entity.
*/
final class RepositoryCollector implements EntityWrapperInterface {
/**
* The collected repositories.
......@@ -35,14 +38,16 @@ final class RepositoryCollector {
}
/**
* Adds a repository to the list.
*
* @param \Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryInterface $repository
* The typed entity repository to collect.
* @param string $entity_type_id
* The entity type ID.
* @param string $bundle
* The bundle name.
* @param string $wrapper_class
* The FQN for the class that will wrap this entity.
* @param string $bundle
* The bundle name.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*
......@@ -91,8 +96,18 @@ final class RepositoryCollector {
return $repository;
}
/**
* {@inheritdoc}
*/
public function wrap(EntityInterface $entity): WrappedEntityInterface {
return $this->repositoryFromEntity($entity)->wrap($entity);
}
/**
* {@inheritdoc}
*/
public function wrapMultiple(array $entities): array {
return array_map([$this, 'wrap'], $entities);
}
}
......@@ -4,4 +4,7 @@ namespace Drupal\typed_entity;
use Exception;
class RepositoryNotFoundException extends Exception {}
\ No newline at end of file
/**
* Custom RepositoryNotFoundException.
*/
class RepositoryNotFoundException extends Exception {}
......@@ -11,6 +11,9 @@ use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use UnexpectedValueException;
/**
* Base class all repositories should extend from.
*/
class TypedEntityRepositoryBase implements TypedEntityRepositoryInterface {
/**
......@@ -58,7 +61,7 @@ class TypedEntityRepositoryBase implements TypedEntityRepositoryInterface {
/**
* RepositoryCollector constructor.
*
* @param $container
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container.
*/
public function __construct(ContainerInterface $container) {
......@@ -177,6 +180,12 @@ class TypedEntityRepositoryBase implements TypedEntityRepositoryInterface {
}
/**
* Wraps multiple entities by entity ID.
*
* Note that even when the entities are all of the same type there is no
* guarantee that they are all of the same bundle. That means that different
* wrapped entity classes may be returned.
*
* @param array $items
* The array containing the IDs of the entities to wrap.
*
......
......@@ -2,40 +2,14 @@
namespace Drupal\typed_entity\TypedRepositories;
use Drupal\Component\Assertion\Inspector;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
use Drupal\typed_entity\EntityWrapperInterface;
interface TypedEntityRepositoryInterface {
/**
* Wraps an entity with business logic.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to wrap.
*
* @return \Drupal\typed_entity\WrappedEntities\WrappedEntityInterface
* The wrapped entity.
*
* @throws \Drupal\typed_entity\InvalidValueException
*/
public function wrap(EntityInterface $entity): WrappedEntityInterface;
/**
* Wraps an entities with business logic.
*
* @param \Drupal\Core\Entity\EntityInterface[] $entities
* The entity to wrap.
*
* @return \Drupal\typed_entity\WrappedEntities\WrappedEntityInterface[]
* The wrapped entities.
*
* @throws \Drupal\typed_entity\InvalidValueException
*/
public function wrapMultiple(array $entities): array;
/**
* Entity repository.
*/
interface TypedEntityRepositoryInterface extends EntityWrapperInterface {
/**
* Initialize the repository with the parameters in the service container.
......
<?php
namespace Drupal\typed_entity\WrappedEntities;
use Drupal\Core\Entity\EntityInterface;
use Drupal\typed_entity\RepositoryCollector;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class all wrapped entities should extend from.
*/
abstract class WrappedEntityBase implements WrappedEntityInterface {
/**
......@@ -40,19 +41,14 @@ abstract class WrappedEntityBase implements WrappedEntityInterface {
}
/**
* Get the label of the entity.
*
* @return string
* {@inheritdoc}
*/
public function label(): string {
return $this->getEntity()->label();
}
/**
* Gets the owner of the entity.
*
* @return \Drupal\typed_entity\WrappedEntities\WrappedEntityInterface|null
* The owner.
* {@inheritdoc}
*/
public function owner(): ?WrappedEntityInterface {
$owner_key = $this->getEntity()->getEntityType()->getKey('owner');
......
......@@ -3,7 +3,6 @@
namespace Drupal\typed_entity\WrappedEntities;
use Drupal\Core\Entity\EntityInterface;
use Drupal\typed_entity\RepositoryCollector;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -42,6 +41,7 @@ interface WrappedEntityInterface {
* Get the label of the entity.
*
* @return string
* The entity label.
*/
public function label(): string;
......@@ -50,6 +50,8 @@ interface WrappedEntityInterface {
*
* @return \Drupal\typed_entity\WrappedEntities\WrappedEntityInterface|null
* The owner.
*
* @throws \Drupal\typed_entity\InvalidValueException
*/
public function owner(): ?WrappedEntityInterface;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment