Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EntityManager.php
Go to the documentation of this file.
1 <?php
7 
13 
27 {
31  private $operationPool;
32 
36  private $callbackHandler;
37 
44  public function __construct(
45  OperationPool $operationPool,
46  MetadataPool $metadataPool,
47  TypeResolver $typeResolver,
48  CallbackHandler $callbackHandler
49  ) {
50  $this->operationPool = $operationPool;
51  $this->metadataPool = $metadataPool;
52  $this->typeResolver = $typeResolver;
53  $this->callbackHandler = $callbackHandler;
54  }
55 
63  public function load($entity, $identifier, $arguments = [])
64  {
65  $entityType = $this->typeResolver->resolve($entity);
66  $operation = $this->operationPool->getOperation($entityType, 'read');
67  if (!($operation instanceof ReadInterface)) {
68  throw new \LogicException(get_class($operation) . ' must implement ' . ReadInterface::class);
69  }
70  $entity = $operation->execute($entity, $identifier, $arguments);
71  return $entity;
72  }
73 
81  public function save($entity, $arguments = [])
82  {
83  $entityType = $this->typeResolver->resolve($entity);
84  if ($this->has($entity)) {
85  $operation = $this->operationPool->getOperation($entityType, 'update');
86  if (!($operation instanceof UpdateInterface)) {
87  throw new \LogicException(get_class($operation) . ' must implement ' . UpdateInterface::class);
88  }
89  } else {
90  $operation = $this->operationPool->getOperation($entityType, 'create');
91  if (!($operation instanceof CreateInterface)) {
92  throw new \LogicException(get_class($operation) . ' must implement ' . CreateInterface::class);
93  }
94  }
95  try {
96  $entity = $operation->execute($entity, $arguments);
97  $this->callbackHandler->process($entityType);
98  } catch (\Exception $e) {
99  $this->callbackHandler->clear($entityType);
100  throw $e;
101  }
102  return $entity;
103  }
104 
110  public function has($entity)
111  {
112  $entityType = $this->typeResolver->resolve($entity);
113  $operation = $this->operationPool->getOperation($entityType, 'checkIfExists');
114  if (!($operation instanceof CheckIfExistsInterface)) {
115  throw new \LogicException(get_class($operation) . ' must implement ' . CheckIfExistsInterface::class);
116  }
117  return $operation->execute($entity);
118  }
119 
127  public function delete($entity, $arguments = [])
128  {
129  $entityType = $this->typeResolver->resolve($entity);
130  $operation = $this->operationPool->getOperation($entityType, 'delete');
131  if (!($operation instanceof DeleteInterface)) {
132  throw new \LogicException(get_class($operation) . ' must implement ' . DeleteInterface::class);
133  }
134  try {
135  $operation->execute($entity, $arguments);
136  $this->callbackHandler->process($entityType);
137  } catch (\Exception $e) {
138  $this->callbackHandler->clear($entityType);
139  throw $e;
140  }
141  return true;
142  }
143 }
__construct(OperationPool $operationPool, MetadataPool $metadataPool, TypeResolver $typeResolver, CallbackHandler $callbackHandler)
$entity
Definition: element.phtml:22
$arguments
load($entity, $identifier, $arguments=[])