Skip to content

Commit

Permalink
Merge branch 'dev' into unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilien Gilet committed Oct 17, 2017
2 parents 15c648d + 14e0561 commit 1bb801c
Show file tree
Hide file tree
Showing 31 changed files with 1,653 additions and 864 deletions.
44 changes: 44 additions & 0 deletions Annotation/Notifiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Mgilet\NotificationBundle\Annotation;


use Doctrine\Common\Annotations\Annotation;

/**
* Class Notifiable
* @package Mgilet\NotificationBundle\Annotation
*
* @Annotation
* @Annotation\Target("CLASS")
*/
class Notifiable
{
/**
* @Required()
* @var string
*/
public $name;

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

/**
* @param mixed $name
*
* @return Notifiable
*/
public function setName($name)
{
$this->name = $name;

return $this;
}


}
92 changes: 54 additions & 38 deletions Controller/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Mgilet\NotificationBundle\Controller;

use Mgilet\NotificationBundle\Model\AbstractNotification;
use Mgilet\NotificationBundle\Model\UserNotificationInterface;
use Mgilet\NotificationBundle\Entity\Notification;
use Mgilet\NotificationBundle\NotifiableInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

/**
* Class NotificationController
Expand All @@ -20,78 +19,95 @@ class NotificationController extends Controller
/**
* List of all notifications
*
* @Route("/", name="notifications_list")
* @Route("/{notifiable}", name="notification_list")
* @Method("GET")
* @throws \LogicException
* @param NotifiableInterface $notifiable
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listAction()
public function listAction($notifiable)
{
$notifiableRepo = $this->get('doctrine.orm.entity_manager')->getRepository('MgiletNotificationBundle:NotifiableNotification');
return $this->render('MgiletNotificationBundle::notifications.html.twig', array(
'notifications' => $this->get('mgilet.notification')->getUserNotifications($this->getUser())
'notifiableNotifications' => $notifiableRepo->findAllForNotifiableId($notifiable)
));
}

/**
* Set a Notification as seen
*
* @Route("/{notification}/mark_as_seen", name="notification_mark_as_seen")
* @Route("/{notifiable}/mark_as_seen/{notification}", name="notification_mark_as_seen")
* @Method("POST")
* @param AbstractNotification $notification
* @param int $notifiable
* @param Notification $notification
*
* @return JsonResponse
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws \Doctrine\ORM\EntityNotFoundException
* @throws \LogicException
*/
public function markAsSeenAction($notification)
public function markAsSeenAction($notifiable, $notification)
{
$em = $this->getDoctrine()->getEntityManager();
$notification = $this->get('mgilet.notification')->getNotificationById($notification);
$notification->setSeen(true);
$em->persist($notification);
$em->flush();
$manager = $this->get('mgilet.notification');
$manager->markAsSeen(
$manager->getNotifiableInterface($manager->getNotifiableEntityById($notifiable)),
$manager->getNotification($notification),
true
);

return new JsonResponse(true);
}

/**
* Set a Notification as unseen
*
* @Route("/{notification}/mark_as_unseen", name="notification_mark_as_unseen")
* @Route("/{notifiable}/mark_as_unseen/{notification}", name="notification_mark_as_unseen")
* @Method("POST")
* @param AbstractNotification $notification
* @param $notifiable
* @param $notification
*
* @return JsonResponse
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws \Doctrine\ORM\EntityNotFoundException
* @throws \LogicException
*/
public function markAsUnSeenAction($notification)
public function markAsUnSeenAction($notifiable, $notification)
{
$em = $this->getDoctrine()->getEntityManager();
$notification = $this->get('mgilet.notification')->getNotificationById($notification);
$notification->setSeen(false);
$em->persist($notification);
$em->flush();
$manager = $this->get('mgilet.notification');
$manager->markAsUnseen(
$manager->getNotifiableInterface($manager->getNotifiableEntityById($notifiable)),
$manager->getNotification($notification),
true
);

return new JsonResponse(true);
}

/**
* Set all Notifications for a User as seen
*
* @Route("/markAllAsSeen", name="notification_mark_all_as_seen")
* @Route("/{notifiable}/markAllAsSeen", name="notification_mark_all_as_seen")
* @Method("POST")
* @throws \LogicException
* @throws \Symfony\Component\Security\Core\Exception\AuthenticationException
* @param $notifiable
*
* @return JsonResponse
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function markAllAsSeenAction()
public function markAllAsSeenAction($notifiable)
{
$em = $this->getDoctrine()->getEntityManager();
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserNotificationInterface) {
throw new AuthenticationException('This user does not have access to this section.');
}
$notifications = $this->get('mgilet.notification')->getUnseenUserNotifications($user);
foreach ($notifications as $notification) {
$notification->setSeen(true);
$em->persist($notification);
}
$em->flush();
$manager = $this->get('mgilet.notification');
$manager->markAllAsSeen(
$manager->getNotifiableInterface($manager->getNotifiableEntityById($notifiable)),
true
);

return new JsonResponse(true);
}
Expand Down
39 changes: 0 additions & 39 deletions DependencyInjection/Configuration.php

This file was deleted.

8 changes: 3 additions & 5 deletions DependencyInjection/MgiletNotificationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ class MgiletNotificationExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader(
$yamlLoader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.yml');
$yamlLoader->load('services.yml');

$container->setParameter('mgilet_notification.notification_class', $config['notification_class']);
}

}
147 changes: 147 additions & 0 deletions Entity/NotifiableEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Mgilet\NotificationBundle\Entity;


use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* Class NotifiableEntity
* @package Mgilet\NotificationBundle\Entity
*
* @ORM\Table(name="notifiable")
* @ORM\Entity(repositoryClass="Mgilet\NotificationBundle\Entity\Repository\NotifiableRepository")
* @UniqueEntity(fields={"identifier", "class"})
*/
class NotifiableEntity
{
/**
* @var string $id
*
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Id
*/
protected $id;

/**
* @var string $identifier
*
* @ORM\Column(type="string", length=255)
*/
protected $identifier;

/**
* @var string
*
* @ORM\Column(type="string", length=255)
*/
protected $class;

/**
* @var NotifiableNotification[]|ArrayCollection
* @ORM\OneToMany(targetEntity="Mgilet\NotificationBundle\Entity\NotifiableNotification", mappedBy="notifiableEntity",cascade={"persist"})
*/
protected $notifiableNotifications;

/**
* AbstractNotifiableEntity constructor.
*
* @param $identifier
* @param $class
*/
public function __construct($identifier, $class)
{
$this->identifier = $identifier;
$this->class = $class;
$this->notifiableNotifications = new ArrayCollection();
}

/**
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* @return string
*/
public function getIdentifier()
{
return $this->identifier;
}

/**
* @param string $identifier
*
* @return NotifiableEntity
*/
public function setIdentifier($identifier)
{
$this->identifier = $identifier;

return $this;
}

/**
* @return string
*/
public function getClass()
{
return $this->class;
}

/**
* @param string $class
*
* @return NotifiableEntity
*/
public function setClass($class)
{
$this->class = $class;

return $this;
}

/**
* @return ArrayCollection|NotifiableNotification[]
*/
public function getNotifiableNotifications()
{
return $this->notifiableNotifications;
}

/**
* @param NotifiableNotification $notifiableNotification
*
* @return $this
*/
public function addNotifiableNotification(NotifiableNotification $notifiableNotification)
{
if (!$this->notifiableNotifications->contains($notifiableNotification)) {
$this->notifiableNotifications[] = $notifiableNotification;
$notifiableNotification->setNotifiableEntity($this);
}

return $this;
}

/**
* @param NotifiableNotification $notifiableNotification
*
* @return $this
*/
public function removeNotifiableNotification(NotifiableNotification $notifiableNotification)
{
if ($this->notifiableNotifications->contains($notifiableNotification)) {
$this->notifiableNotifications->removeElement($notifiableNotification);
$notifiableNotification->setNotifiableEntity(null);
}

return $this;
}
}
Loading

0 comments on commit 1bb801c

Please sign in to comment.