Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rugbymauri committed Oct 18, 2023
2 parents 9e291f0 + ad9d9b5 commit 989f754
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 97 deletions.
193 changes: 193 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# CrudHistoryBundle

## Basic Usage

### config/routes.yaml
```yaml
whatwedo_crud_history_bundle:
resource: "@whatwedoCrudHistoryBundle/Resources/config/routing.yml"
prefix: /

```

### src/Definition/History/PersonHistoryDefinition.php

```php
<?php

namespace App\Definition\History;

use App\Entity\Email;
use App\Entity\Person;
use App\Entity\Phone;
use whatwedo\CrudHistoryBundle\Definition\HistoryAssociatedClass;
use whatwedo\CrudHistoryBundle\Definition\HistoryDefinitionInterface;

class PersonHistoryDefinition implements HistoryDefinitionInterface
{
public function getMainClass(): string
{
return Person::class;
}

/**
* @return HistoryAssociatedClass[]
*/
public function getAssociatedClasses(): array
{
/**
* Classes that are Associated the main Class
*/
return [
new HistoryAssociatedClass(Email::class, []),
new HistoryAssociatedClass(Phone::class, []),
];
}

public function getChildDefinitions(): array
{
return [];
}
}
```


### src/Definition/PersonDefinition.php
```php

public static function getCapabilities(): array
{
return array_merge(
[ HistoryPage::HISTORY ],,
parent::getCapabilities()
);

}
```

## Many To One

### src/Entity/Contact.php
```php
namespace whatwedo\CrudHistoryBundle\Tests\App\Entity;

use App\Repository\ContactRepository;
use Doctrine\ORM\Mapping as ORM;
use whatwedo\CrudHistoryBundle\Entity\AuditManyToOneTriggerInterface;

#[ORM\Entity(repositoryClass: ContactRepository::class)]
class Contact implements \Stringable, AuditManyToOneTriggerInterface
{
#[ORM\OneToMany(targetEntity: 'Company', mappedBy: 'contacts')]
private Company $company;

public function __construct(Company $company)
{
$this->company = $company;
}

// ...

public function triggerManyToOne(): array
{
$this->getCompany()->triggerAudit();

return [$this->getCompany()];
}
}
```

The `triggerManyToOne` should return all entities where the audit has been triggered (`triggerAudit`). This array can contain `null` values.
This allows you to call getters inside this array and save you some space for null checks.

### src/Entity/Company.php
```php
namespace whatwedo\CrudHistoryBundle\Tests\App\Entity;

use App\Repository\CompanyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use whatwedo\CrudHistoryBundle\Entity\AuditTriggerInterface;
use whatwedo\CrudHistoryBundle\Entity\AuditTriggerTrait;

#[ORM\Entity(repositoryClass: CompanyRepository::class)]
class Company implements AuditTriggerInterface
{
use AuditTriggerTrait;

#[ORM\OneToMany(targetEntity: 'Contact', mappedBy: 'company')]
private Collection $contacts;

public function __construct()
{
$this->contacts = new ArrayCollection();
}

// ...
}
```

### src/Definition/CompanyDefinition.php
```php
namespace whatwedo\CrudHistoryBundle\Tests\App\Definition;

use araise\CrudBundle\Definition\AbstractDefinition;
use whatwedo\CrudHistoryBundle\Definition\HasHistoryDefinition;
use whatwedo\CrudHistoryBundle\Tests\App\Definition\History\CompanyHistoryDefinition;
use whatwedo\CrudHistoryBundle\Tests\App\Entity\Company;

class CompanyDefinition extends AbstractDefinition implements HasHistoryDefinition
{
public static function getEntity(): string
{
return Company::class;
}

public static function getCapabilities(): array
{
return array_merge(
[HistoryPage::HISTORY],
parent::getCapabilities()
);
}

public function getHistoryDefinition(): string
{
return CompanyHistoryDefinition::class;
}
}
```

### App/Definition/History/CompanyHistoryDefinition.php
```php
namespace whatwedo\CrudHistoryBundle\Tests\App\Definition\History;

use whatwedo\CrudHistoryBundle\Definition\HistoryAssociatedClass;
use whatwedo\CrudHistoryBundle\Definition\HistoryDefinitionInterface;
use whatwedo\CrudHistoryBundle\Tests\App\Entity\Company;
use whatwedo\CrudHistoryBundle\Tests\App\Entity\Contact;

class CompanyHistoryDefinition implements HistoryDefinitionInterface
{
public function getMainClass(): string
{
return Company::class;
}

/**
* @return HistoryAssociatedClass[]
*/
public function getAssociatedClasses(): array
{
return [
new HistoryAssociatedClass(Contact::class, []),
];
}

public function getChildDefinitions(): array
{
return [];
}
}
```
71 changes: 0 additions & 71 deletions readme.md

This file was deleted.

7 changes: 6 additions & 1 deletion src/Entity/AuditManyToOneTriggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

interface AuditManyToOneTriggerInterface
{
public function triggerManyToOne();
/**
* Can be used to trigger related entity objects
*
* @return array holds all triggered objects. Will be used to save the audit increment. You can write null values to this array, this allows you to use getters very easily.
*/
public function triggerManyToOne(): array;
}
2 changes: 1 addition & 1 deletion src/Entity/AuditTriggerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait AuditTriggerTrait
#[ORM\Column(type: 'integer')]
private int $auditTrigger = 0;

public function triggerAudit()
public function triggerAudit(): void
{
++$this->auditTrigger;
}
Expand Down
48 changes: 26 additions & 22 deletions src/EventSubscriber/AuditTriggerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,49 @@

use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use whatwedo\CrudHistoryBundle\Entity\AuditManyToOneTriggerInterface;

final class AuditTriggerSubscriber implements EventSubscriberInterface
{
public function __construct(
protected EntityManagerInterface $entityManager
) {
}

public function getSubscribedEvents(): array
{
return [
Events::prePersist,
Events::preUpdate,
Events::postFlush,
Events::onFlush,
];
}

public function preUpdate($entity): void
public function onFlush(OnFlushEventArgs $args): void
{
$this->triggerManyToOneAssciations($entity);
}
$entityManager = $args->getObjectManager();
$unitOfWork = $entityManager->getUnitOfWork();

public function prePersist($entity): void
{
$this->triggerManyToOneAssciations($entity);
}
$updatedEntities = $unitOfWork->getScheduledEntityUpdates();
$insertedEntities = $unitOfWork->getScheduledEntityInsertions();

public function postFlush($entity): void
{
return;
if (count($updatedEntities) > 0 || count($insertedEntities) > 0) {
$this->handleEntities($insertedEntities, $entityManager);
$this->handleEntities($updatedEntities, $entityManager);
}
}

private function triggerManyToOneAssciations(LifecycleEventArgs $eventArgs): void
/**
* @param object[] $entities
*/
private function handleEntities(array $entities, EntityManagerInterface $entityManager): void
{
if ($eventArgs->getEntity() instanceof AuditManyToOneTriggerInterface) {
$eventArgs->getEntity()->triggerManyToOne();
foreach ($entities as $entity) {
if ($entity instanceof AuditManyToOneTriggerInterface) {
$touchedEntities = $entity->triggerManyToOne();

foreach ($touchedEntities as $touchedEntity) {
if ($touchedEntity === null) {
continue;
}
$entityManager->getUnitOfWork()->recomputeSingleEntityChangeSet($entityManager->getClassMetadata(get_class($touchedEntity)), $touchedEntity);
}
}
}
}
}
4 changes: 3 additions & 1 deletion tests/App/Entity/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ public function setCompany(Company $company): void
$this->company = $company;
}

public function triggerManyToOne()
public function triggerManyToOne(): array
{
$this->getCompany()->triggerAudit();

return [$this->getCompany()];
}

public function __toString(): string
Expand Down
2 changes: 1 addition & 1 deletion tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testGetHistory(): void
]);
$crawler = $client->request('GET', $url);

$this->assertResponseIsSuccessful();
self::assertResponseIsSuccessful();

$this->assertnotSame('', $crawler->html());
}
Expand Down

0 comments on commit 989f754

Please sign in to comment.