<repo_name>GabrielBourdette/symfony<file_sep>/src/Controller/ProductController.php
<?php
namespace App\Controller;

use App\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;


class ProductController extends AbstractController
{
	
	protected $NB_PER_PAGE = 6;

	public function __construct(SessionInterface $session)
    {
        $this->session = $session;
    }

	/**
	 * @Route("/product/{page}", name="product_index", requirements={"page" = "\d+"}, defaults={"page" = 1})
	 */
	public function list_products($page) {

	    $repository = $this->getDoctrine()
	      ->getManager()
	      ->getRepository('App:Product');

	    $listProducts = $repository->getProducts($page, $this->NB_PER_PAGE);
	    $nbPages = ceil(count($listProducts) / $this->NB_PER_PAGE);

	    return $this->render('Product/index.html.twig', array(
	      'listProducts' => $listProducts,
	      'nbPages'     => $nbPages,
	      'page'        => $page,
	      'cardQuantity' => ($this->session->get('card_quantity') > 0) ? $this->session->get('card_quantity') : 0
	    ));
	}



  	/**
	 * @Route("/product/view/{slug}", name="product_view")
	 */
	public function view_product($slug)
	  {

	  	if(empty($slug)) {
		    return $this->redirectToRoute('product_index', array(
		      'listProducts' => $this->listProducts
		    ));
		}

	    $repository = $this->getDoctrine()
	      ->getManager()
	      ->getRepository('App:Product');

	    $product = $repository->findOneBy(array('slug' => $slug));

	    if (null === $product) {
	      throw new NotFoundHttpException("Le produit n'existe pas.");
	    }

	    return $this->render('Product/view.html.twig', array(
	      'product' => $product,
	      'cardQuantity' => ($this->session->get('card_quantity') > 0) ? $this->session->get('card_quantity') : 0
	    ));
	  }



}
<file_sep>/tests/Controller/CardContollerTest.php
<?php
namespace App\Tests\Controller;

use App\Controller\CardController;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;


class MainControllerTest extends WebTestCase
{
	

	public function test_home() {

		$client = static::createClient();
        $crawler = $client->request('GET', '/product');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        echo "\n Code réponse de la page Accueil : ".$client->getResponse()->getStatusCode();

        $this->assertCount(6, $crawler->filter('.product_bloc'));
        echo "\n Nombre de produits sur la page Accueil : ".$crawler->filter('.product_bloc')->count();

	}


	// commande : php bin/phpunit


}<file_sep>/src/Repository/ProductRepository.php
<?php
namespace App\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;

class ProductRepository extends EntityRepository
{
  
  public function getProducts($page, $nbPerPage)
  {
    $query = $this->createQueryBuilder('p')
      ->orderBy('p.title', 'ASC')
      ->getQuery();

    $query
      ->setFirstResult(($page-1) * $nbPerPage)
      ->setMaxResults($nbPerPage);

    return new Paginator($query, true);
  }
}<file_sep>/src/Controller/CardController.php
<?php
namespace App\Controller;

use App\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;


class CardController extends AbstractController
{
	
	protected $total;
	protected $listProducts;
	protected $listProducts_qt;

	public function __construct(SessionInterface $session) {

		$this->total = 0;
		$this->listProducts = array();
		$this->cardQuantity = 0;
		$this->session = $session;
	}


	/**
	 * @Route("/card", name="card_index")
	 */
	public function card_view() {


	   	$session_list = is_array($this->session->get('products_list')) ? $this->session->get('products_list') : array() ;
	   	$products_quantities = array();

	   	if(count($session_list) >= 1) {
		   	foreach ($session_list as $product_id => $product_qt) {

		   		$new_product = $repository = $this->getDoctrine()
			      ->getManager()
			      ->getRepository('App:Product')
			      ->find($product_id);

			    $this->listProducts[] = $new_product;
			    $products_quantities[$product_id] = $product_qt;
			    $this->total += $new_product->getPrice() * $product_qt;
			    $this->cardQuantity += $product_qt;
		   	}
		}

	   	$this->session->set('card_quantity',$this->cardQuantity);

	    return $this->render('Card/card.html.twig', array(
	      'listProducts' => $this->listProducts,
	      'productsQuantities' => $products_quantities,
	      'cardTotal' => $this->total,
	      'cardQuantity' => $this->session->get('card_quantity')
	    ));
	}


	/**
   	 * @Route("/card/add/{id_product}", name="add_to_card")
   	 */
	public function card_add($id_product) {

	   	$session_list = $this->session->get('products_list');
	   	$product_qt = isset($_GET['product_qt']) ? $_GET['product_qt'] : 1;
	   	$session_list[$id_product] = (isset($session_list[$id_product]) ? $session_list[$id_product] : 0) + $product_qt;

	    $this->session->set('products_list', $session_list);
	    $this->session->set('card_quantity', $this->session->get('card_quantity') + $product_qt);

	    $this->session->getFlashBag()->add('infos', 'Produit ajouté au panier !');
	    return $this->redirectToRoute('card_index', array(
	      'listProducts' => $this->listProducts
	    ));
	}


	/**
   	 * @Route("/card/one_more/{id_product}", name="card_one_more")
   	 */
	public function card_one_more($id_product) {

	   	$session_list = $this->session->get('products_list');
	   	$session_list[$id_product] = $session_list[$id_product] + 1;

	    $this->session->set('products_list', $session_list);
	    $this->session->set('card_quantity', $this->session->get('card_quantity') + 1);

	    $this->session->getFlashBag()->add('infos', 'Produit ajouté au panier !');
	    return $this->redirectToRoute('card_index', array(
	      'listProducts' => $this->listProducts
	    ));
	}


	/**
   	 * @Route("/card/one_less/{id_product}", name="card_one_less")
   	 */
	public function card_one_less($id_product) {

	   	$session_list = $this->session->get('products_list');
	   	$session_list[$id_product] = $session_list[$id_product] - 1;

	    $this->session->set('products_list', $session_list);
	    $this->session->set('card_quantity', $this->session->get('card_quantity') - 1);

	    $this->session->getFlashBag()->add('infos', 'Produit retiré du panier !');
	    return $this->redirectToRoute('card_index', array(
	      'listProducts' => $this->listProducts
	    ));
	}


	/**
   	 * @Route("/card/remove/{id_product}", name="card_remove_one")
   	 */
	public function card_remove_one($id_product) {

	   	$session_list = $this->session->get('products_list');
	   	$previous_qt = $session_list[$id_product];
	   	unset($session_list[$id_product]);

	    $this->session->set('products_list', $session_list);
	    $this->session->set('card_quantity', $this->session->get('card_quantity') - $previous_qt);

	    $this->session->getFlashBag()->add('infos', 'Produit retiré du panier !');
	    return $this->redirectToRoute('card_index', array(
	      'listProducts' => $this->listProducts
	    ));
	}


	/**
   	 * @Route("/card/remove_all", name="empty_card")
   	 */
	public function card_delete() {

	    $this->session->set('products_list', array());
	    $this->session->set('card_quantity', 0);

	    $this->session->getFlashBag()->add('infos', 'Panier vide !');
	    return $this->redirectToRoute('card_index', array(
	      'listProducts' => $this->listProducts,
	      'cardQuantity' => count($this->session->get('products_list'))
	    ));
	}



}<file_sep>/src/DataFixtures/AppFixtures.php
<?php

namespace App\DataFixtures;

use App\Entity\Product;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class AppFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        
        $product = new Product('espadrilles_multicolores', 'espadrilles multicolores', 'bleu jaune vert rouge', 25.0, 'multicolore.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_bleues', 'espadrilles bleues', 'bleu bleu beu', 15.50, 'bleu.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_jaunes', 'espadrilles jaunes', 'jaune jaune jaune', 19.50, 'jaune.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_rouges', 'espadrilles rouges', 'rouge rouge rouge', 18.50, 'rouge.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_vertes', 'espadrilles vertes', 'vert vert vert', 19.50, 'vert.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_noires', 'espadrilles noires', 'noir noir noir', 19.0, 'noir.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_blanches', 'espadrilles blanches', 'blanc blanc blanc', 21.50, 'blanc.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_blanches_bleues', 'espadrilles blanches et bleues', 'blanc bleu blanc', 14.50, 'bleu_blanc.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_blanches_rouges', 'espadrilles blanches et rouges', 'blanc rouge blanc', 20.50, 'rouge_blanc.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_beiges', 'espadrilles beiges', 'beige beige beige', 16.50, 'beige.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_violettes', 'espadrilles violettes', 'violet violet violet', 19.50, 'violet.jpg');
        $manager->persist($product);

        $product = new Product('espadrilles_oranges', 'espadrilles oranges', 'orange orange orange', 19.0, 'orange.jpg');
        $manager->persist($product);

        $manager->flush();


        // commande : php bin/console doctrine:fixtures:load
    }
}
<file_sep>/src/Entity/Product.php
<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Product
{
  /**
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;

   /**
   * @ORM\Column(name="slug", type="string", length=150)
   */
  protected $slug;

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

  /**
   * @ORM\Column(name="price", type="float")
   */
  protected $price;

  /**
   * @ORM\Column(name="description", type="text")
   */
  protected $description;

  /**
   * @ORM\Column(name="img", type="text")
   */
  protected $img;

  public function setSlug($slug) { $this->slug = $slug; }
  public function setTitle($title) { $this->title = $title; }
  public function setPrice($price) { $this->price = $price; }
  public function setDescription($description) { $this->description = $description; }
  public function setImg($img) { $this->img = $img; }

  public function getId() { return $this->id; }
  public function getSlug() { return $this->slug; }
  public function getTitle() { return $this->title; }
  public function getPrice() { return $this->price; }
  public function getDescription() { return $this->description; }
  public function getImg() { return $this->img; }



  public function __construct($slug = "", $title = "", $desc = "", $price = 0, $img = "") {

    $this->setSlug($slug);
    $this->setTitle($title);
    $this->setDescription($desc);
    $this->setPrice($price);
    $this->setImg($img);
  }


}