• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ipublikuj / doctrine-crud / 10245045887

05 Aug 2024 08:03AM UTC coverage: 31.907%. First build
10245045887

push

github

web-flow
Merge pull request #8 from ipublikuj/feature/throw-exception

Throw dbal exception

0 of 2 new or added lines in 2 files covered. (0.0%)

164 of 514 relevant lines covered (31.91%)

2.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/src/Crud/Update/EntityUpdater.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * EntityUpdater.php
5
 *
6
 * @copyright      More in LICENSE.md
7
 * @license        https://www.ipublikuj.eu
8
 * @author         Adam Kadlec <adam.kadlec@ipublikuj.eu>
9
 * @package        iPublikuj:DoctrineCrud!
10
 * @subpackage     Crud
11
 * @since          1.0.0
12
 *
13
 * @date           29.01.14
14
 */
15

16
namespace IPub\DoctrineCrud\Crud\Update;
17

18
use Doctrine\DBAL;
19
use Doctrine\ORM;
20
use Doctrine\Persistence;
21
use IPub\DoctrineCrud\Crud;
22
use IPub\DoctrineCrud\Entities;
23
use IPub\DoctrineCrud\Exceptions;
24
use IPub\DoctrineCrud\Mapping;
25
use Nette\Utils;
26

27
/**
28
 * Doctrine CRUD entity updater
29
 *
30
 * @template   T of Entities\IEntity
31
 * @extends    Crud\CrudManager<T>
32
 *
33
 * @package        iPublikuj:DoctrineCrud!
34
 * @subpackage     Crud
35
 *
36
 * @author         Adam Kadlec <adam.kadlec@ipublikuj.eu>
37
 */
38
class EntityUpdater extends Crud\CrudManager
39
{
40

41
        /** @var array<callable(Entities\IEntity, Utils\ArrayHash): void> */
42
        public array $beforeAction = [];
43

44
        /** @var array<callable(Entities\IEntity, Utils\ArrayHash): void> */
45
        public array $afterAction = [];
46

47
        /**
48
         * @param class-string<T> $entityName
49
         */
50
        public function __construct(
51
                string $entityName,
52
                private readonly Mapping\IEntityMapper $entityMapper,
53
                Persistence\ManagerRegistry $managerRegistry,
54
        )
55
        {
56
                parent::__construct($entityName, $managerRegistry);
3✔
57
        }
58

59
        /**
60
         * @throws DBAL\Exception\UniqueConstraintViolationException
61
         * @throws Exceptions\InvalidArgument
62
         * @throws Exceptions\InvalidState
63
         */
64
        public function update(Utils\ArrayHash $values, Entities\IEntity|int|string $entity): Entities\IEntity
65
        {
66
                if (!$entity instanceof Entities\IEntity) {
3✔
67
                        $entity = $this->entityRepository->find($entity);
×
68
                }
69

70
                if (!$entity instanceof Entities\IEntity) {
3✔
71
                        throw new Exceptions\InvalidArgument('Entity not found.');
×
72
                }
73

74
                Utils\Arrays::invoke($this->beforeAction, $entity, $values);
3✔
75

76
                $this->entityMapper->fillEntity($values, $entity, false);
3✔
77

78
                $this->entityManager->persist($entity);
3✔
79

80
                Utils\Arrays::invoke($this->afterAction, $entity, $values);
3✔
81

82
                if ($this->getFlush() === true) {
3✔
83
                        try {
84
                                $this->entityManager->flush();
3✔
NEW
85
                        } catch (ORM\Exception\ORMException $ex) {
×
86
                                throw new Exceptions\InvalidState('Entity could not be updated', $ex->getCode(), $ex);
×
87
                        }
88
                }
89

90
                return $entity;
3✔
91
        }
92

93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc