<commit_before><?php
/**
 * Shopware 4.0
 * Copyright © 2012 shopware AG
 *
 * According to our dual licensing model, this program can be used either
 * under the terms of the GNU Affero General Public License, version 3,
 * or under a proprietary license.
 *
 * The texts of the GNU Affero General Public License with an additional
 * permission and of our proprietary license can be found at and
 * in the LICENSE file you have received along with this program.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * "Shopware" is a registered trademark of shopware AG.
 * The licensing of the program under the AGPLv3 does not imply a
 * trademark license. Therefore any rights, title and interest in
 * our trademarks remain entirely with us.
 */

namespace Shopware\Components\Api\Resource;

use Shopware\Components\Api\Exception as ApiException;
use Shopware\Models\Article\Article as ArticleModel;


/**
 * Article API Resource
 */
class Article extends Resource
{
    /**
     * @return \Shopware\Models\Article\Repository
     */
    public function getRepository()
    {
        return $this->getManager()->getRepository('Shopware\Models\Article\Article');
    }

    /**
     * @return \Shopware\Models\Article\Repository
     */
    public function getDetailRepository()
    {
        return $this->getManager()->getRepository('Shopware\Models\Article\Detail');
    }

    /**
     * @param int $id
     * @return array|\Shopware\Models\Article\Article
     * @throws \Shopware\Components\Api\Exception\ParameterMissingException
     * @throws \Shopware\Components\Api\Exception\NotFoundException
     */
    public function getOne($id)
    {
        $this->checkPrivilege('read');

        if (empty($id)) {
            throw new ApiException\ParameterMissingException();
        }

        $builder = $this->getManager()->createQueryBuilder();
        $builder->select(array(
            'article',
            'configuratorSet',
            'configuratorSetGroups',
            'mainDetail',
            'PARTIAL categories.{id, name}',
            'PARTIAL similar.{id, name}',
            'PARTIAL accessories.{id, name}',
            'images',
            'links',
            'downloads',
            'tax',
            'customerGroups',
            'propertyValues',
            'supplier',
            'mainDetailAttribute',
            'propertyGroup',
            'details',
            'configuratorOptions',
        ))
        ->from('Shopware\Models\Article\Article', 'article')
        ->leftJoin('article.configuratorSet', 'configuratorSet')
        ->leftJoin('configuratorSet.groups', 'configuratorSetGroups')
        ->leftJoin('article.mainDetail', 'mainDetail')
        ->leftJoin('article.tax', 'tax')
        ->leftJoin('article.categories', 'categories', null, null, 'categories.id')
        ->leftJoin('article.links', 'links')
        ->leftJoin('article.images', 'images')
        ->leftJoin('article.downloads', 'downloads')
        ->leftJoin('article.related', 'accessories')
        ->leftJoin('article.propertyValues', 'propertyValues')
        ->leftJoin('article.similar', 'similar')
        ->leftJoin('article.customerGroups', 'customerGroups')
        ->leftJoin('article.supplier', 'supplier')
        ->leftJoin('article.details', 'details', 'WITH', 'details.kind = 2')
        ->leftJoin('mainDetail.attribute', 'mainDetailAttribute')
        ->leftJoin('article.propertyGroup', 'propertyGroup')
        ->leftJoin('details.configuratorOptions', 'configuratorOptions')
        ->where('article.id = ?1')
        ->andWhere('images.parentId IS NULL')
        ->setParameter(1, $id);

        /** @var $article \Shopware\Models\Article\Article */
        $article = $builder->getQuery()->getOneOrNullResult($this->getResultMode());

        if (!$article) {
            throw new ApiException\NotFoundException("Article by id $id not found");
        }

        if ($this->getResultMode() == self::HYDRATE_ARRAY) {
            $query = $this->getManager()->createQuery('SELECT shop FROM Shopware\Models\Shop\Shop as shop');
            $shops = $query->getArrayResult();

            $translationReader = new \Shopware_Components_Translation();
            foreach ($shops as $shop) {
                $translation = $translationReader->read($shop['id'], 'article', $id);
                if (!empty($translation)) {
                    $translation['shopId'] = $shop['id'];
                    $article['translations'][$shop['id']] = $translation;
                }
            }
        }

        return $article;
    }

    /**
     * @param int $offset
     * @param int $limit
     * @param array $criteria
     * @param array $orderBy
     * @return array
     */
    public function getList($offset = 0, $limit = 25, array $criteria = array(), array $orderBy = array())
    {
        $this->checkPrivilege('read');

        $builder = $this->getRepository()->createQueryBuilder('article');

        $builder->addFilter($criteria)
                ->addOrderBy($orderBy)
                ->setFirstResult($offset)
                ->setMaxResults($limit);

        $query = $builder->getQuery();

        $query->setHydrationMode($this->getResultMode());

        $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);

        //returns the total count of the query
        $totalResult = $paginator->count();

        //returns the article data
        $articles = $paginator->getIterator()->getArrayCopy();

        return array('data' => $articles, 'total' => $totalResult);
    }

    /**
     * @param array $params
     * @return \Shopware\Models\Article\Article
     * @throws \Shopware\Components\Api\Exception\CustomValidationException
     * @throws \Shopware\Components\Api\Exception\ValidationException
     */
    public function create(array $params)
    {
        $this->checkPrivilege('create');<commit_msg>SW-3825 - Join over mainDetail for articleResource->getList<commit_after><?php
/**
 * Shopware 4.0
 * Copyright © 2012 shopware AG
 *
 * According to our dual licensing model, this program can be used either
 * under the terms of the GNU Affero General Public License, version 3,
 * or under a proprietary license.
 *
 * The texts of the GNU Affero General Public License with an additional
 * permission and of our proprietary license can be found at and
 * in the LICENSE file you have received along with this program.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * "Shopware" is a registered trademark of shopware AG.
 * The licensing of the program under the AGPLv3 does not imply a
 * trademark license. Therefore any rights, title and interest in
 * our trademarks remain entirely with us.
 */

namespace Shopware\Components\Api\Resource;

use Shopware\Components\Api\Exception as ApiException;
use Shopware\Models\Article\Article as ArticleModel;


/**
 * Article API Resource
 */
class Article extends Resource
{
    /**
     * @return \Shopware\Models\Article\Repository
     */
    public function getRepository()
    {
        return $this->getManager()->getRepository('Shopware\Models\Article\Article');
    }

    /**
     * @return \Shopware\Models\Article\Repository
     */
    public function getDetailRepository()
    {
        return $this->getManager()->getRepository('Shopware\Models\Article\Detail');
    }

    /**
     * @param int $id
     * @return array|\Shopware\Models\Article\Article
     * @throws \Shopware\Components\Api\Exception\ParameterMissingException
     * @throws \Shopware\Components\Api\Exception\NotFoundException
     */
    public function getOne($id)
    {
        $this->checkPrivilege('read');

        if (empty($id)) {
            throw new ApiException\ParameterMissingException();
        }

        $builder = $this->getManager()->createQueryBuilder();
        $builder->select(array(
            'article',
            'configuratorSet',
            'configuratorSetGroups',
            'mainDetail',
            'PARTIAL categories.{id, name}',
            'PARTIAL similar.{id, name}',
            'PARTIAL accessories.{id, name}',
            'images',
            'links',
            'downloads',
            'tax',
            'customerGroups',
            'propertyValues',
            'supplier',
            'mainDetailAttribute',
            'propertyGroup',
            'details',
            'configuratorOptions',
        ))
        ->from('Shopware\Models\Article\Article', 'article')
        ->leftJoin('article.configuratorSet', 'configuratorSet')
        ->leftJoin('configuratorSet.groups', 'configuratorSetGroups')
        ->leftJoin('article.mainDetail', 'mainDetail')
        ->leftJoin('article.tax', 'tax')
        ->leftJoin('article.categories', 'categories', null, null, 'categories.id')
        ->leftJoin('article.links', 'links')
        ->leftJoin('article.images', 'images')
        ->leftJoin('article.downloads', 'downloads')
        ->leftJoin('article.related', 'accessories')
        ->leftJoin('article.propertyValues', 'propertyValues')
        ->leftJoin('article.similar', 'similar')
        ->leftJoin('article.customerGroups', 'customerGroups')
        ->leftJoin('article.supplier', 'supplier')
        ->leftJoin('article.details', 'details', 'WITH', 'details.kind = 2')
        ->leftJoin('mainDetail.attribute', 'mainDetailAttribute')
        ->leftJoin('article.propertyGroup', 'propertyGroup')
        ->leftJoin('details.configuratorOptions', 'configuratorOptions')
        ->where('article.id = ?1')
        ->andWhere('images.parentId IS NULL')
        ->setParameter(1, $id);

        /** @var $article \Shopware\Models\Article\Article */
        $article = $builder->getQuery()->getOneOrNullResult($this->getResultMode());

        if (!$article) {
            throw new ApiException\NotFoundException("Article by id $id not found");
        }

        if ($this->getResultMode() == self::HYDRATE_ARRAY) {
            $query = $this->getManager()->createQuery('SELECT shop FROM Shopware\Models\Shop\Shop as shop');
            $shops = $query->getArrayResult();

            $translationReader = new \Shopware_Components_Translation();
            foreach ($shops as $shop) {
                $translation = $translationReader->read($shop['id'], 'article', $id);
                if (!empty($translation)) {
                    $translation['shopId'] = $shop['id'];
                    $article['translations'][$shop['id']] = $translation;
                }
            }
        }

        return $article;
    }

    /**
     * @param int $offset
     * @param int $limit
     * @param array $criteria
     * @param array $orderBy
     * @return array
     */
    public function getList($offset = 0, $limit = 25, array $criteria = array(), array $orderBy = array())
    {
        $this->checkPrivilege('read');

        $builder = $this->getRepository()->createQueryBuilder('article')
            ->leftJoin('article.mainDetail', 'mainDetail');

        $builder->addFilter($criteria)
                ->addOrderBy($orderBy)
                ->setFirstResult($offset)
                ->setMaxResults($limit);

        $query = $builder->getQuery();

        $query->setHydrationMode($this->getResultMode());

        $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);

        //returns the total count of the query
        $totalResult = $paginator->count();

        //returns the article data
        $articles = $paginator->getIterator()->getArrayCopy();

        return array('data' => $articles, 'total' => $totalResult);
    }

    /**
     * @param array $params
     * @return \Shopware\Models\Article\Article
     * @throws \Shopware\Components\Api\Exception\CustomValidationException
     * @throws \Shopware\Components\Api\Exception\ValidationException
     */
    public function create(array $params)
    {
        $this->checkPrivilege('create');
{'fraction_non_alphanumeric': 0.13438542259822664, 'fraction_numerical': 0.002603107459529814, 'mean_word_length': 2.594736842105263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '130133', 'n_tokens_mistral': 3314, 'n_tokens_neox': 3201, 'n_words': 784}