<commit_before><?php

namespace CanalTP\MediaManager\Storage;

use CanalTP\MediaManager\Company\CompanyInterface;
use CanalTP\MediaManager\Strategy\StrategyInterface;
use CanalTP\MediaManager\Category\CategoryInterface;
use CanalTP\MediaManager\Media\MediaInterface;
use CanalTP\MediaManager\Storage\AbstractStorage;
use CanalTP\MediaManager\Media\Builder\MediaBuilder;

class FileSystem extends AbstractStorage
{
    public function addMedia(
        MediaInterface $media,
        StrategyInterface $strategy
        ) {
        $result = false;
        $path = $this->getPath();
        $path .= $strategy->generatePath($media);

        if (!is_dir(dirname($path))) {
            mkdir(dirname($path), 0777, true);
        }
        if ($result = rename($media->getPath(), $path)) {
            $media->setPath($path);
        }

        return ($result);
    }

    public function getMediasByCategory(
        CompanyInterface $company,
        StrategyInterface $strategy,
        CategoryInterface $category
        ) {
        $files = $strategy->getMediasPathByCategory($company, $category);
        $mediaBuilder = new MediaBuilder();

        foreach ($files as $file) {
            $category->addMedia(
                $mediaBuilder->buildMedia(
                    $file,<commit_msg>Stockage with FileSystem updated. (Rename function failed with NFS)<commit_after><?php

namespace CanalTP\MediaManager\Storage;

use CanalTP\MediaManager\Company\CompanyInterface;
use CanalTP\MediaManager\Strategy\StrategyInterface;
use CanalTP\MediaManager\Category\CategoryInterface;
use CanalTP\MediaManager\Media\MediaInterface;
use CanalTP\MediaManager\Storage\AbstractStorage;
use CanalTP\MediaManager\Media\Builder\MediaBuilder;

class FileSystem extends AbstractStorage
{
    public function addMedia(
        MediaInterface $media,
        StrategyInterface $strategy
        ) {
        $result = false;
        $path = $this->getPath();
        $path .= $strategy->generatePath($media);

        if (!is_dir(dirname($path))) {
            mkdir(dirname($path), 0777, true);
        }

        // TODO: Remove this when rename function will be patched --> https://bugs.php.net/bug.php?id=54097
        $cmd = 'mv ' . $media->getPath() . ' ' . $path;
        exec($cmd);
        $media->setPath($path);
        $result = true;
        // if ($result = rename($media->getPath(), $path)) {
        //     $media->setPath($path);
        // }

        return ($result);
    }

    public function getMediasByCategory(
        CompanyInterface $company,
        StrategyInterface $strategy,
        CategoryInterface $category
        ) {
        $files = $strategy->getMediasPathByCategory($company, $category);
        $mediaBuilder = new MediaBuilder();

        foreach ($files as $file) {
            $category->addMedia(
                $mediaBuilder->buildMedia(
                    $file,
{'fraction_non_alphanumeric': 0.11870255348516218, 'fraction_numerical': 0.004485852311939268, 'mean_word_length': 2.6465408805031445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 1, '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': '634100', 'n_tokens_mistral': 797, 'n_tokens_neox': 764, 'n_words': 157}