<commit_before><?php

namespace Product\Controller;

use Zend\Mvc\Controller\AbstractRestfulController;
use Product\Model\Image;
use Product\Model\Mapper\ProductHydrator;

/**
 * Description of ProductController
 *
 * @author wangting
 */
class ProductController extends AbstractRestfulController
{

    protected $productMapper;
    protected $productImageMapper;

    public function getList()
    {
        $products = $this->getProductMapper()->fetchAll();
        $productHydrate = new ProductHydrator();
        foreach($products as $product) {
            $productArray[] = $productHydrate->extract($product);
        }
        return $productArray;
    }

    /**
     * Return single resource
     *
     * @param mixed $id
     * @return mixed
     */
    public function get($id)
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('product', array(
                    'action' => 'add',
                ));
        }
        $product = $this->getProductMapper()->findById($id);
        $productHydrate = new ProductHydrator();
        return $productHydrate->extract($product);
    }

    /**
     * Create a new resource
     *
     * @param mixed $data
     * @return mixed
     */
    public function create($data)
    {
        $message = 'creating new product';
        $this->getEventManager()->trigger('info', $this, array('message' => $message));

        $form = $this->getServiceLocator()->get('ProductForm');
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData(array(
                'name' => $data->name, 'price' => $data->price,
            ));
            if ($form->isValid()) {
                $mapper = $this->getProductMapper();
                $product = $mapper->insert($form->getData());
                foreach (range(1, 3) as $index) {
                    $this->_saveFile($index, $product->getId());
                }
                $productHydrate = new ProductHydrator();
                return $productHydrate->extract($product);
            } else {
                throw new \Exception('form not valid'. print_r($form->getMessages(), true));
            }
        } else {
            throw new \Exception('can not save');
        }
    }

    /**
     * Update an existing resource
     *
     * @param mixed $id
     * @param mixed $data
     * @return mixed
     */
    public function update($id, $data)
    {

    }

    /**
     * Delete an existing resource
     *
     * @param  mixed $id
     * @return mixed
     */
    public function delete($id)
    {

    }

    public function getProductMapper()
    {
        if (!$this->productMapper) {
            $sm = $this->getServiceLocator();
            $this->productMapper = $sm->get('Product\Model\Mapper\Product');
        }
        return $this->productMapper;
    }

    public function getProductImageMapper()
    {
        if (!$this->productImageMapper) {
            $sm = $this->getServiceLocator();
            $this->productImageMapper = $sm->get('Product\Model\Mapper\Image');
        }
        return $this->productImageMapper;
    }

    public function indexAction()
    {
        $products = $this->getProductMapper()->fetchAll();
        //var_dump($this->params()->fromRoute('page'));
        $products->setCurrentPageNumber($this->params()->fromRoute('page'));
        return array('products' => $products);
    }

    public function addAction()
    {<commit_msg>add module for unit test.<commit_after><?php

namespace Product\Controller;

use Zend\Mvc\Controller\AbstractRestfulController;
use Product\Model\Image;
use Product\Model\Mapper\ProductHydrator;

/**
 * Description of ProductController
 *
 * @author wangting
 */
class ProductController extends AbstractRestfulController
{

    protected $productMapper;
    protected $productImageMapper;

    protected function info($message)
    {
        $this->getEventManager()->trigger('info', $this, array('message' => $message));
    }

    public function getList()
    {
        $products = $this->getProductMapper()->fetchAll();
        $products->setCurrentPageNumber(1);
        $this->info(get_class($products));
        $productHydrate = new ProductHydrator();
        foreach($products as $product) {
            $productArray[] = $productHydrate->extract($product);
        }
        return $productArray;
    }

    /**
     * Return single resource
     *
     * @param mixed $id
     * @return mixed
     */
    public function get($id)
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('product', array(
                    'action' => 'add',
                ));
        }
        $product = $this->getProductMapper()->findById($id);
        $productHydrate = new ProductHydrator();
        return $productHydrate->extract($product);
    }

    /**
     * Create a new resource
     *
     * @param mixed $data
     * @return mixed
     */
    public function create($data)
    {
        $message = 'creating new product';
        $this->getEventManager()->trigger('info', $this, array('message' => $message));

        $form = $this->getServiceLocator()->get('ProductForm');
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData(array(
                'name' => $data->name, 'price' => $data->price,
            ));
            if ($form->isValid()) {
                $mapper = $this->getProductMapper();
                $product = $mapper->insert($form->getData());
                foreach (range(1, 3) as $index) {
                    $this->_saveFile($index, $product->getId());
                }
                $productHydrate = new ProductHydrator();
                return $productHydrate->extract($product);
            } else {
                throw new \Exception('form not valid'. print_r($form->getMessages(), true));
            }
        } else {
            throw new \Exception('can not save');
        }
    }

    /**
     * Update an existing resource
     *
     * @param mixed $id
     * @param mixed $data
     * @return mixed
     */
    public function update($id, $data)
    {

    }

    /**
     * Delete an existing resource
     *
     * @param  mixed $id
     * @return mixed
     */
    public function delete($id)
    {

    }


    public function setProductMapper($productMapper)
    {
        $this->productMapper = $productMapper;
        return $this;
    }
    public function getProductMapper()
    {
        if (!$this->productMapper) {
            $sm = $this->getServiceLocator();
            $this->productMapper = $sm->get('Product\Model\Mapper\Product');
        }
        return $this->productMapper;
    }

    public function getProductImageMapper()
    {
        if (!$this->productImageMapper) {
            $sm = $this->getServiceLocator();
            $this->productImageMapper = $sm->get('Product\Model\Mapper\Image');
        }
        return $this->productImageMapper;
    }

    public function indexAction()
    {
        $products = $this->getProductMapper()->fetchAll();
        //var_dump($this->params()->fromRoute('page'));
        $products->setCurrentPageNumber($this->params()->fromRoute('page'));
        return array('products' => $products);
    }

    public function addAction()
    {
{'fraction_non_alphanumeric': 0.1489390458170023, 'fraction_numerical': 0.000946073793755913, 'mean_word_length': 2.056588186699711, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '825951', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1996, 'n_words': 404}