You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
848B

  1. <?php
  2. namespace common\logic;
  3. use common\logic\Producer\Producer\Model\Producer;
  4. use yii\base\ErrorException;
  5. trait ProducerContextTrait
  6. {
  7. protected ?Producer $producerContext;
  8. public function setProducerContext(Producer $producer = null): self
  9. {
  10. $this->producerContext = $producer;
  11. return $this;
  12. }
  13. public function getProducerContext(bool $throwExceptionIfNull = true): ?Producer
  14. {
  15. if($throwExceptionIfNull && is_null($this->producerContext)) {
  16. throw new ErrorException("Le contexte producteur n'est pas défini.");
  17. }
  18. return $this->producerContext;
  19. }
  20. public function getProducerContextId()
  21. {
  22. return $this->getProducerContext()->id;
  23. }
  24. public function isOutOfProducerContext(): bool
  25. {
  26. return !$this->producerContext;
  27. }
  28. }