<commit_before><?php

/**
 * @package mindgruve/derby
 * @copyright (c) 2015 Mindgruve
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

namespace Derby\Media;

use Derby\Adapter\LocalFileAdapterInterface;
use Derby\AdapterInterface;
use Derby\Media\LocalFile;
use Derby\MediaInterface;

/**
 * Derby\Media\Provider
 *
 * This class and its children may go away after we have a service container
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
abstract class AbstractLocalFileFactory implements LocalFileFactoryInterface
{
    /**
     * Supported extensions
     * @var array
     */
    protected $extensions;

    /**
     * Supported mime types
     * @var array
     */
    protected $mimeTypes;

    /**
     * {@inheritDoc}
     */
    abstract public function build($key, LocalFileAdapterInterface $adapter);

    /**
     * @param array $extensions
     * @param array $mimetypes
     */
    public function __construct(array $extensions, array $mimetypes)
    {
        $this->setExtensions($extensions);
        $this->setMimeTypes($mimetypes);
    }

    /**
     * {@inheritDoc}
     */
    public function setExtensions(array $extensions)
    {
        $this->extensions = $extensions;
    }

    /**
     * {@inheritDoc}
     */
    public function setMimeTypes(array $mimeTypes)
    {
        $this->mimeTypes  = $mimeTypes;
    }

    /**
     * {@inheritDoc}
     */
    public function supports(MediaInterface $media)
    {

        if (!$media instanceof LocalFile) {
            return false;
        }
        
        $matchExt = false;
        foreach ($this->extensions as $extension) {
            if (fnmatch($extension, $media->getFileExtension())) {
                $matchExt = true;
            }
        }
        
        $matchMimeType = false;
        foreach ($this->mimeTypes as $mimeType) {
            if (fnmatch($mimeType, $media->getMimeType())) {
                $matchMimeType = true;
            }
        }

        return $matchExt && $matchMimeType;
    }
}<commit_msg>Files can be supported by mime type only when no extension exists<commit_after><?php

/**
 * @package mindgruve/derby
 * @copyright (c) 2015 Mindgruve
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

namespace Derby\Media;

use Derby\Adapter\LocalFileAdapterInterface;
use Derby\AdapterInterface;
use Derby\Media\LocalFile;
use Derby\MediaInterface;

/**
 * Derby\Media\Provider
 *
 * This class and its children may go away after we have a service container
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
abstract class AbstractLocalFileFactory implements LocalFileFactoryInterface
{
    /**
     * Supported extensions
     * @var array
     */
    protected $extensions;

    /**
     * Supported mime types
     * @var array
     */
    protected $mimeTypes;

    /**
     * {@inheritDoc}
     */
    abstract public function build($key, LocalFileAdapterInterface $adapter);

    /**
     * @param array $extensions
     * @param array $mimetypes
     */
    public function __construct(array $extensions, array $mimetypes)
    {
        $this->setExtensions($extensions);
        $this->setMimeTypes($mimetypes);
    }

    /**
     * {@inheritDoc}
     */
    public function setExtensions(array $extensions)
    {
        $this->extensions = $extensions;
    }

    /**
     * {@inheritDoc}
     */
    public function setMimeTypes(array $mimeTypes)
    {
        $this->mimeTypes  = $mimeTypes;
    }

    /**
     * {@inheritDoc}
     */
    public function supports(MediaInterface $media)
    {

        if (!$media instanceof LocalFile) {
            return false;
        }
        
        $matchExt = false;
        foreach ($this->extensions as $extension) {
            if (fnmatch($extension, $media->getFileExtension())) {
                $matchExt = true;
            }
        }
        
        $matchMimeType = false;
        foreach ($this->mimeTypes as $mimeType) {
            if (fnmatch($mimeType, $media->getMimeType())) {
                $matchMimeType = true;
            }
        }

        return $media->getFileExtension() == ''
            ? $matchMimeType === true
            : ($matchExt && $matchMimeType) === true;
    }
}
{'fraction_non_alphanumeric': 0.1280718336483932, 'fraction_numerical': 0.001890359168241966, 'mean_word_length': 2.1660433807030666, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 44, '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': '6873315', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1208, 'n_words': 281}