/**
  * Test getter/setter for id property
  */
 public function testGetSetId()
 {
     $this->assertEmpty($this->channel->getId());
     // change value and assert new
     $newId = 5;
     $this->assertEntity($this->channel->setId($newId));
     $this->assertEquals($newId, $this->channel->getId());
 }
 /**
  * Checks if the given completeness is in the given channel.
  *
  * @param AbstractCompleteness $completeness
  * @param Channel              $channel
  *
  * @return bool
  */
 protected function isInChannel(AbstractCompleteness $completeness, Channel $channel)
 {
     if ($completeness->getChannel()->getId() === $channel->getId()) {
         return true;
     }
     return false;
 }
 /**
  * Is the given product complete for the given channel?
  *
  * @param ProductInterface $product
  * @param Channel          $channel
  *
  * @return bool
  */
 protected function isProductComplete(ProductInterface $product, Channel $channel)
 {
     $completenesses = $product->getCompletenesses()->toArray();
     foreach ($completenesses as $completeness) {
         if ($completeness->getChannel()->getId() === $channel->getId() && $completeness->getRatio() === 100) {
             return true;
         }
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 public function buildByChannelAndCompleteness(Channel $channel)
 {
     $scope = $channel->getCode();
     $qb = $this->buildByScope($scope);
     $rootAlias = $qb->getRootAlias();
     $expression = 'pCompleteness.product = ' . $rootAlias . ' AND ' . $qb->expr()->eq('pCompleteness.ratio', '100') . ' AND ' . $qb->expr()->eq('pCompleteness.channel', $channel->getId());
     $rootEntity = current($qb->getRootEntities());
     $completenessMapping = $this->_em->getClassMetadata($rootEntity)->getAssociationMapping('completenesses');
     $completenessClass = $completenessMapping['targetEntity'];
     $qb->innerJoin($completenessClass, 'pCompleteness', 'WITH', $expression);
     $treeId = $channel->getCategory()->getId();
     $expression = $qb->expr()->eq('pCategory.root', $treeId);
     $qb->innerJoin($rootAlias . '.categories', 'pCategory', 'WITH', $expression);
     return $qb;
 }
    /**
     * Get the deleted locales of a channel (the channel is updated but not flushed yet).
     *
     * @param Channel $channel
     *
     * @return array the list of deleted locales
     */
    public function getDeletedLocaleIdsForChannel(Channel $channel)
    {
        $currentLocaleIds = array_map(function ($locale) {
            return $locale->getId();
        }, $channel->getLocales()->toArray());
        $sql = <<<SQL
    SELECT cl.locale_id
    FROM pim_catalog_channel_locale cl
    WHERE cl.channel_id = :channel_id
      AND cl.locale_id NOT IN (:current_locale_ids)
SQL;
        $stmt = $this->getEntityManager()->getConnection()->executeQuery($sql, array(':channel_id' => $channel->getId(), ':current_locale_ids' => $currentLocaleIds), array(':current_locale_ids' => Connection::PARAM_INT_ARRAY));
        $rows = $stmt->fetchAll();
        $locales = array_map(function ($row) {
            return (int) $row['locale_id'];
        }, $rows);
        return $locales;
    }
 function it_returns_only_products_in_the_given_channel(ProductInterface $product1, ProductInterface $product2, Channel $channel, ArrayCollection $productCategories1, ArrayCollection $productCategories2, ArrayCollection $completenesses, Category $rootCategory, Category $category1, Category $category2, AbstractCompleteness $completeness1, AbstractCompleteness $completeness2)
 {
     $channel->getCategory()->willReturn($rootCategory);
     $channel->getId()->willReturn(2);
     $rootCategory->getId()->willReturn(1);
     $product1->getCategories()->willReturn($productCategories1);
     $product2->getCategories()->willReturn($productCategories2);
     $product1->getCompletenesses()->willReturn($completenesses);
     $product2->getCompletenesses()->willReturn($completenesses);
     $completenesses->toArray()->willReturn([$completeness1, $completeness2]);
     $completeness1->getChannel()->willReturn($channel);
     $completeness2->getChannel()->willReturn($channel);
     $completeness1->getRatio()->willReturn(100);
     $completeness2->getRatio()->willReturn(100);
     $productCategories1->toArray()->willReturn([$category1]);
     $productCategories2->toArray()->willReturn([$category2]);
     $category1->getRoot()->willReturn(1);
     $category2->getRoot()->willReturn(5);
     $this->apply($channel, [$product1, $product2])->shouldReturn([$product1]);
 }
 /**
  * Schedule recalculation of completenesses for all products
  * of a channel
  *
  * @param Channel $channel
  */
 public function scheduleForChannel(Channel $channel)
 {
     if ($channel->getId()) {
         $deletedLocaleIds = $this->channelRepository->getDeletedLocaleIdsForChannel($channel);
         foreach ($deletedLocaleIds as $deletedLocaleId) {
             $deletedLocale = $this->localeRepository->find($deletedLocaleId);
             $this->generator->scheduleForChannelAndLocale($channel, $deletedLocale);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function scheduleForChannelAndLocale(Channel $channel, Locale $locale)
 {
     $productQb = $this->documentManager->createQueryBuilder($this->productClass);
     $pullExpr = $productQb->expr()->addAnd($productQb->expr()->field('channel')->equals($channel->getId()))->addAnd($productQb->expr()->field('locale')->equals($locale->getId()));
     $productQb->update()->multiple(true)->field(sprintf('normalizedData.completenesses.%s-%s', $channel->getCode(), $locale->getCode()))->unsetField()->field('completenesses')->pull($pullExpr)->getQuery()->execute();
 }
    /**
     * {@inheritdoc}
     */
    public function scheduleForChannelAndLocale(Channel $channel, Locale $locale)
    {
        $sql = <<<SQL
            DELETE c FROM pim_catalog_completeness c
            WHERE c.channel_id = :channel_id
            AND c.locale_id = :locale_id
SQL;
        $sql = $this->applyTableNames($sql);
        $stmt = $this->connection->prepare($sql);
        $stmt->bindValue('channel_id', $channel->getId());
        $stmt->bindValue('locale_id', $locale->getId());
        $stmt->execute();
    }
    /**
     * Get the deleted locales of a channel (the channel is updated but not flushed yet).
     *
     * @param Channel $channel
     *
     * @return array the list of deleted locales
     */
    public function getDeletedLocalesForChannel(Channel $channel)
    {
        $currentLocaleIds = array_map(function ($locale) {
            return $locale->getId();
        }, $channel->getLocales()->toArray());
        $dql = <<<DQL
            SELECT l
            JOIN l.channels c
            WHERE c.id = :channel_id
              AND l.id NOT IN (:current_locale_ids)
DQL;
        $query = $this->getEntityManager()->createQuery($dql)->setParameter(':channel_id', $channel->getId())->setParameter(':current_locale_ids', implode(',', $currentLocaleIds));
        return $query->getResult();
    }
 /**
  * {@inheritDoc}
  */
 public function getId()
 {
     if ($this->__isInitialized__ === false) {
         return (int) parent::getId();
     }
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', array());
     return parent::getId();
 }