From 604c5cabd17ab86ed4853765fc30a4966a457adc Mon Sep 17 00:00:00 2001 From: Kayn Ty Date: Thu, 24 May 2018 11:57:53 +0200 Subject: [PATCH] service for default values --- app/config/services.yml | 7 +- .../Controller/DefaultController.php | 72 ++--------- src/AppBundle/Entity/Festival.php | 17 +++ src/AppBundle/Service/OwnerService.php | 122 ++++++++++++++++++ 4 files changed, 154 insertions(+), 64 deletions(-) create mode 100644 src/AppBundle/Service/OwnerService.php diff --git a/app/config/services.yml b/app/config/services.yml index 6dcfb9f9..67e0489b 100755 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -9,7 +9,7 @@ services: # default configuration for services in *this* file _defaults: # automatically injects dependencies in your services - autowire: false + autowire: true # automatically registers your services as commands, event subscribers, etc. autoconfigure: true # this means you cannot fetch services directly from the container via $container->get() @@ -49,3 +49,8 @@ services: - '@fos_user.user_manager' - ['@fos_user.user_manager', twitter: twitter_id] - '@doctrine.orm.default_entity_manager' + tk.owner.service: + autowire: false + class: AppBundle\Service\OwnerService + arguments: + - '@doctrine.orm.default_entity_manager' diff --git a/src/AppBundle/Controller/DefaultController.php b/src/AppBundle/Controller/DefaultController.php index 306dbd32..b6462456 100755 --- a/src/AppBundle/Controller/DefaultController.php +++ b/src/AppBundle/Controller/DefaultController.php @@ -7,6 +7,7 @@ use AppBundle\Entity\Product; use AppBundle\Entity\ProductCategory; use AppBundle\Entity\ProductSold; use AppBundle\Entity\SellRecord; +use AppBundle\Service\OwnerService; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse; @@ -17,11 +18,13 @@ use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; class DefaultController extends Controller { + private $ownerService; private $tokenManager; - public function __construct(CsrfTokenManagerInterface $tokenManager = null) + public function __construct(CsrfTokenManagerInterface $tokenManager = null, OwnerService $ownerService) { $this->tokenManager = $tokenManager; + $this->ownerService = $ownerService; } /** @@ -111,68 +114,20 @@ class DefaultController extends Controller $currentUser = $this->getUser(); if (!$currentUser) { return new JsonResponse([ - 'categories' => [['name' => 'demo category', 'products' => []]], + 'categories' => [[]], 'recentSells' => [['']], ]); } + $ownerService = $this->ownerService; + $ownerService->setupNewFestival($currentUser); $activeFestival = $currentUser->getActiveFestival(); - if (!$activeFestival) { - $activeFestival = $m->getRepository('AppBundle:Festival') - ->findOneBy(['user' => $this->getUser()->getId()], - ['id' => 'desc'], - 0, - 1); - if (!$activeFestival) { - $activeFestival = new Festival(); - $activeFestival->setDateCreation(new \DateTime()) - ->setName('default festival') - ->setChiffreAffaire(0) - ->setFondDeCaisseAvant(0) - ->setFondDeCaisseApres(0) - ->setUser($currentUser); - } - $currentUser->setActiveFestival($activeFestival); - $m->persist($activeFestival); - $m->persist($currentUser); - $m->flush(); - } - $categRepo = $m->getRepository('AppBundle:ProductCategory'); $sellingRepo = $m->getRepository('AppBundle:SellRecord'); - $categories = $currentUser->getCategories(); - if (!count($categories)) { - $categories = $defaultCategories = $categRepo->findById([1, 2]); - $currentUser->setCategories($defaultCategories); - $m->persist($currentUser); - } - $serializedCategories = []; - foreach ($categories as $category) { - $products = $category->getProducts(); - if ($products) { - $listOfProductsInArray = []; - foreach ($products as $product) { - $listOfProductsInArray[] = [ - 'id' => $product->getId(), - 'name' => $product->getName(), - 'category' => $category->getId(), - 'price' => 1 * $product->getPrice(), - 'stockCount' => $product->getStockCount(), - ]; - } - $products = $listOfProductsInArray; - } - $serializedCategories[] = - [ - 'id' => $category->getId(), - 'name' => $category->getName(), - 'products' => $products ? $products : null, - ]; - } - $categories = $serializedCategories; + $categories = $ownerService->serializeCategoriesOfUser($currentUser); $recentSells = $sellingRepo->findBy(['user' => $currentUser->getId()], ['id' => 'desc'], 0, 5); @@ -180,16 +135,7 @@ class DefaultController extends Controller return new JsonResponse([ 'categories' => $categories, 'recentSells' => count($recentSells), - 'lastFestival' => [ - 'id' => $activeFestival->getId(), - 'name' => $activeFestival->getName(), - 'commentaire' => $activeFestival->getComment(), - 'dateCreation' => $activeFestival->getDateCreation(), - 'chiffreAffaire' => $activeFestival->getChiffreAffaire(), - 'clientsCount' => count($activeFestival->getSellRecords()), - 'fondDeCaisseAvant' => $activeFestival->getFondDeCaisseAvant(), - 'fondDeCaisseApres' => $activeFestival->getFondDeCaisseApres(), - ], + 'lastFestival' => $activeFestival->makeArray(), ]); } diff --git a/src/AppBundle/Entity/Festival.php b/src/AppBundle/Entity/Festival.php index e2c7207a..e694c66d 100755 --- a/src/AppBundle/Entity/Festival.php +++ b/src/AppBundle/Entity/Festival.php @@ -83,6 +83,23 @@ class Festival { private $fraisRepas; + /** + * array usable by js + * @return array + */ + public function makeArray(){ + return [ + 'id' => $this->getId(), + 'name' => $this->getName(), + 'commentaire' => $this->getComment(), + 'dateCreation' => $this->getDateCreation(), + 'chiffreAffaire' => $this->getChiffreAffaire(), + 'clientsCount' => count($this->getSellRecords()), + 'fondDeCaisseAvant' => $this->getFondDeCaisseAvant(), + 'fondDeCaisseApres' => $this->getFondDeCaisseApres(), + ]; + + } public function recalculateChiffreAffaire() { $sellings = $this->getSellRecords(); $newChiffreAffaire = 0; diff --git a/src/AppBundle/Service/OwnerService.php b/src/AppBundle/Service/OwnerService.php new file mode 100644 index 00000000..0b49959a --- /dev/null +++ b/src/AppBundle/Service/OwnerService.php @@ -0,0 +1,122 @@ +em = $em; + } + + + /** + * add default categories for a newly created user + * @return mixed + */ + public function setupNewDefaultCategories( User $user ) { + + $categNames = [ 'livres', 'badges', 'dessins' ]; + foreach ( $categNames as $categ_name ) { + $newCateg = new ProductCategory(); + $newCateg->setName( $categ_name ); + $user->addCategory( $newCateg ); + $newCateg->addUser( $user ); + $this->em->persist( $newCateg ); + $this->em->persist( $user ); + } + $this->em->flush(); + + return $user; + } + + /** + * setup a default festival if needed + * + * @param User $user + * + * @return User + */ + public function setupNewFestival( User $user ) { + + $activeFestival = $user->getActiveFestival(); + if ( ! $activeFestival ) { + $activeFestival = $this->em->getRepository( 'AppBundle:Festival' ) + ->findOneBy( [ 'user' => $this->getUser()->getId() ], + [ 'id' => 'desc' ], + 0, + 1 ); + if ( ! $activeFestival ) { + $activeFestival = new Festival(); + $activeFestival->setDateCreation( new \DateTime() ) + ->setName( 'default festival' ) + ->setChiffreAffaire( 0 ) + ->setFondDeCaisseAvant( 0 ) + ->setFondDeCaisseApres( 0 ) + ->setUser( $user ); + } + $user->setActiveFestival( $activeFestival ); + $this->em->persist( $activeFestival ); + $this->em->persist( $user ); + $this->em->flush(); + } + + return $user; + } + + /** + * transform categories of a user in an array usable by js + * @param User $user + * + * @return array + */ + public function serializeCategoriesOfUser( User $user ) { + $categRepo = $this->em->getRepository( 'AppBundle:ProductCategory' ); + $categories = $user->getCategories(); + if ( ! count( $categories ) ) { + $categories = $defaultCategories = $categRepo->findById( [ 1, 2 ] ); + $user->setCategories( $defaultCategories ); + $this->em->persist( $user ); + } + $serializedCategories = []; + + foreach ( $categories as $category ) { + $products = $category->getProducts(); + if ( $products ) { + $listOfProductsInArray = []; + foreach ( $products as $product ) { + $listOfProductsInArray[] = [ + 'id' => $product->getId(), + 'name' => $product->getName(), + 'category' => $category->getId(), + 'price' => 1 * $product->getPrice(), + 'stockCount' => $product->getStockCount(), + ]; + } + $products = $listOfProductsInArray; + } + $serializedCategories[] = + [ + 'id' => $category->getId(), + 'name' => $category->getName(), + 'products' => $products ? $products : null, + ]; + + } + + return $serializedCategories; + } +}