<commit_before><?php
namespace Vivo\CMS\Api;

use Vivo\CMS\Model;
use Vivo\Repository\RepositoryInterface;
use Vivo\Repository\Exception\EntityNotFoundException;
use Vivo\Storage\PathBuilder\PathBuilderInterface;
use Vivo\CMS\Exception;
use Vivo\Uuid\GeneratorInterface as UuidGeneratorInterface;
use Vivo\CMS\Model\Content\Hyperlink;
use Vivo\CMS\Model\ContentContainer;

use DateTime;

/**
 * Document
 * Document API
 */
class Document implements DocumentInterface
{
    /**
     * Repository
     * @var RepositoryInterface
     */
    protected $repository;

    /**
     * Path Builder
     * @var PathBuilderInterface
     */
    protected $pathBuilder;

    /**
     * CMS API
     * @var CMS
     */
    protected $cmsApi;

    /**
     * UUID Generator
     * @var UuidGeneratorInterface
     */
    protected $uuidGenerator;

    /**
     * @var array
     */
    protected $options = array();

    /**
     * Constructor
     * @param CMS $cmsApi
     * @param \Vivo\Repository\RepositoryInterface $repository
     * @param \Vivo\Storage\PathBuilder\PathBuilderInterface $pathBuilder
     * @param \Vivo\Uuid\GeneratorInterface $uuidGenerator
     * @param array $options
     */
    public function __construct(CMS $cmsApi,
                                RepositoryInterface $repository,
                                PathBuilderInterface $pathBuilder,
                                UuidGeneratorInterface $uuidGenerator,
                                array $options)
    {
        $this->cmsApi           = $cmsApi;
        $this->repository       = $repository;
        $this->pathBuilder      = $pathBuilder;
        $this->uuidGenerator    = $uuidGenerator;
        $this->options = array_merge($this->options, $options);
    }

    /**
     * Returns array of published contents of given document.
     * @param Model\Document $document
     * @return Model\Content[]
     */
    public function getPublishedContents(Model\Document $document)
    {
        $containers = $this->repository->getChildren($document, 'Vivo\CMS\Model\ContentContainer');
        $contents   = array();
        usort($containers,
            function (Model\ContentContainer $a, Model\ContentContainer $b)
            {
                return $a->getOrder() > $b->getOrder();
            });
        foreach ($containers as $container) {
            if ($content = $this->getPublishedContent($container)) {
                $contents[] = $content;
            }
        }
        return $contents;
    }

    /**
     * Returns array of published content types (class names of published contents)
     * If there are no published contents, returns an empty array
     * @param \Vivo\CMS\Model\Document $document
     * @return string[]
     */
    public function getPublishedContentTypes(Model\Document $document)
    {
        $publishedContents      = $this->getPublishedContents($document);
        $publishedContentTypes  = array();
        /** @var $publishedContent Model\Content */
        foreach ($publishedContents as $publishedContent) {
            $publishedContentTypes[]    = get_class($publishedContent);
        }
        return $publishedContentTypes;
    }

    /**
     * Finds published content in ContentContainer,
     * @param Model\ContentContainer $container
     * @return Model\Content|boolean
     * @throws Exception\LogicException when there is more than one published content
     */
    public function getPublishedContent(Model\ContentContainer $container)
    {
        $result = array();
        $contents = $this->repository->getChildren($container, 'Vivo\CMS\Model\Content');
        foreach ($contents as $content) {
            /* @var $content Model\Content */
            if ($content->getState() == 'PUBLISHED') {
                $result[] = $content;
            }
        }
        if (count($result) == 1) {
            return $result[0];
        } elseif (count($result) == 0) {
            return false;
        } else {
            throw new Exception\LogicException(
                sprintf("%s: The ContentContainer '%s' contains more than one published content.",
                    __METHOD__, $container->getPath()));
        }
    }

    /**
     * @param Model\Content $content
     */
    public function publishContent(Model\Content $content)
    {
        $document   = $this->getContentDocument($content);
        $oldContent = $this->getPublishedContent($document, $content->getIndex());
        if ($oldContent) {
            $oldContent->setState('ARCHIVED');
            $this->cmsApi->saveEntity($oldContent, false);
        }
        $content->setState('PUBLISHED');
        $this->cmsApi->saveEntity($content, true);
    }

    /**
     * @return array
     */
    public function getWorkflowStates()
    {
        return $this->options['states'];
    }

    /**
     * Returns all principals workflow states.
     * @return array
     */
    public function getWorkflowAvailableStates()
    {
        return $this->options['states']; //TODO: apply security
    }

     * Sets a workflow state to the content
     * @param Model\Content $content
     * @param string $state
     * @throws \Vivo\CMS\Exception\InvalidArgumentException
     */
    public function setWorkflowState(Model\Content $content, $state)
    {
        $states = $this->getWorkflowStates();
        if (!in_array($state, $states)) {
            throw new Exception\InvalidArgumentException(
                sprintf('%s: Unknown state value; Available: %s', __METHOD__, implode(', ', $states)));
        }
        //TODO: authorization
        if (true /* uzivatel ma pravo na change*/) {

        }
        if ($state == 'PUBLISHED') {
            $this->publishContent($content);
        } else {<commit_msg>Fix /**<commit_after><?php
namespace Vivo\CMS\Api;

use Vivo\CMS\Model;
use Vivo\Repository\RepositoryInterface;
use Vivo\Repository\Exception\EntityNotFoundException;
use Vivo\Storage\PathBuilder\PathBuilderInterface;
use Vivo\CMS\Exception;
use Vivo\Uuid\GeneratorInterface as UuidGeneratorInterface;
use Vivo\CMS\Model\Content\Hyperlink;
use Vivo\CMS\Model\ContentContainer;

use DateTime;

/**
 * Document
 * Document API
 */
class Document implements DocumentInterface
{
    /**
     * Repository
     * @var RepositoryInterface
     */
    protected $repository;

    /**
     * Path Builder
     * @var PathBuilderInterface
     */
    protected $pathBuilder;

    /**
     * CMS API
     * @var CMS
     */
    protected $cmsApi;

    /**
     * UUID Generator
     * @var UuidGeneratorInterface
     */
    protected $uuidGenerator;

    /**
     * @var array
     */
    protected $options = array();

    /**
     * Constructor
     * @param CMS $cmsApi
     * @param \Vivo\Repository\RepositoryInterface $repository
     * @param \Vivo\Storage\PathBuilder\PathBuilderInterface $pathBuilder
     * @param \Vivo\Uuid\GeneratorInterface $uuidGenerator
     * @param array $options
     */
    public function __construct(CMS $cmsApi,
                                RepositoryInterface $repository,
                                PathBuilderInterface $pathBuilder,
                                UuidGeneratorInterface $uuidGenerator,
                                array $options)
    {
        $this->cmsApi           = $cmsApi;
        $this->repository       = $repository;
        $this->pathBuilder      = $pathBuilder;
        $this->uuidGenerator    = $uuidGenerator;
        $this->options = array_merge($this->options, $options);
    }

    /**
     * Returns array of published contents of given document.
     * @param Model\Document $document
     * @return Model\Content[]
     */
    public function getPublishedContents(Model\Document $document)
    {
        $containers = $this->repository->getChildren($document, 'Vivo\CMS\Model\ContentContainer');
        $contents   = array();
        usort($containers,
            function (Model\ContentContainer $a, Model\ContentContainer $b)
            {
                return $a->getOrder() > $b->getOrder();
            });
        foreach ($containers as $container) {
            if ($content = $this->getPublishedContent($container)) {
                $contents[] = $content;
            }
        }
        return $contents;
    }

    /**
     * Returns array of published content types (class names of published contents)
     * If there are no published contents, returns an empty array
     * @param \Vivo\CMS\Model\Document $document
     * @return string[]
     */
    public function getPublishedContentTypes(Model\Document $document)
    {
        $publishedContents      = $this->getPublishedContents($document);
        $publishedContentTypes  = array();
        /** @var $publishedContent Model\Content */
        foreach ($publishedContents as $publishedContent) {
            $publishedContentTypes[]    = get_class($publishedContent);
        }
        return $publishedContentTypes;
    }

    /**
     * Finds published content in ContentContainer,
     * @param Model\ContentContainer $container
     * @return Model\Content|boolean
     * @throws Exception\LogicException when there is more than one published content
     */
    public function getPublishedContent(Model\ContentContainer $container)
    {
        $result = array();
        $contents = $this->repository->getChildren($container, 'Vivo\CMS\Model\Content');
        foreach ($contents as $content) {
            /* @var $content Model\Content */
            if ($content->getState() == 'PUBLISHED') {
                $result[] = $content;
            }
        }
        if (count($result) == 1) {
            return $result[0];
        } elseif (count($result) == 0) {
            return false;
        } else {
            throw new Exception\LogicException(
                sprintf("%s: The ContentContainer '%s' contains more than one published content.",
                    __METHOD__, $container->getPath()));
        }
    }

    /**
     * @param Model\Content $content
     */
    public function publishContent(Model\Content $content)
    {
        $document   = $this->getContentDocument($content);
        $oldContent = $this->getPublishedContent($document, $content->getIndex());
        if ($oldContent) {
            $oldContent->setState('ARCHIVED');
            $this->cmsApi->saveEntity($oldContent, false);
        }
        $content->setState('PUBLISHED');
        $this->cmsApi->saveEntity($content, true);
    }

    /**
     * @return array
     */
    public function getWorkflowStates()
    {
        return $this->options['states'];
    }

    /**
     * Returns all principals workflow states.
     * @return array
     */
    public function getWorkflowAvailableStates()
    {
        return $this->options['states']; //TODO: apply security
    }

    /**
     * Sets a workflow state to the content
     * @param Model\Content $content
     * @param string $state
     * @throws \Vivo\CMS\Exception\InvalidArgumentException
     */
    public function setWorkflowState(Model\Content $content, $state)
    {
        $states = $this->getWorkflowStates();
        if (!in_array($state, $states)) {
            throw new Exception\InvalidArgumentException(
                sprintf('%s: Unknown state value; Available: %s', __METHOD__, implode(', ', $states)));
        }
        //TODO: authorization
        if (true /* uzivatel ma pravo na change*/) {

        }
        if ($state == 'PUBLISHED') {
            $this->publishContent($content);
        } else {
{'fraction_non_alphanumeric': 0.11821974965229486, 'fraction_numerical': 0.000521557719054242, 'mean_word_length': 2.166804293971924, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '6803333', 'n_tokens_mistral': 3081, 'n_tokens_neox': 2955, 'n_words': 689}