<commit_before><?php
/**
 * This file is part of the Imbo package
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE file that was
 * distributed with this source code.
 */

namespace Imbo\Resource;

use Imbo\EventManager\EventInterface,
    Imbo\Exception\InvalidArgumentException,
    Imbo\Model;

/**
 * Metadata resource
 *
 * @author <NAME> <<EMAIL>>
 * @package Resources
 */
class Metadata implements ResourceInterface {
    /**
     * {@inheritdoc}
     */
    public function getAllowedMethods() {
        return array('GET', 'POST', 'PUT', 'DELETE', 'HEAD');
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        return array(
            'metadata.head' => 'get',
            'metadata.get' => 'get',
            'metadata.post' => array(
                'post',
                'validateMetadata' => 10,
            ),
            'metadata.put' => array(
                'put',
                'validateMetadata' => 10,
            ),
            'metadata.delete' => 'delete',
        );
    }

    /**
     * Handle DELETE requests
     *
     * @param EventInterface $event The current event
     */
    public function delete(EventInterface $event) {
        $event->getManager()->trigger('db.metadata.delete');

        $model = new Model\ArrayModel();
        $model->setData(array(
            'imageIdentifier' => $event->getRequest()->getImageIdentifier(),
        ));

        $event->getResponse()->setModel($model);
    }

    /**
     * Handle PUT requests
     *
     * @param EventInterface $event The current event
     */
    public function put(EventInterface $event) {
        $request = $event->getRequest();

        $event->getManager()
            ->trigger('db.metadata.delete')
            ->trigger('db.metadata.update', array(
                'metadata' => json_decode($request->getContent(), true),
            ));

        $model = new Model\ArrayModel();
        $model->setData(array(
            'imageIdentifier' => $request->getImageIdentifier(),
        ));

        $event->getResponse()->setModel($model);
    }

    /**
     * Handle POST requests
     *
     * @param EventInterface $event The current event
     */
    public function post(EventInterface $event) {
        $request = $event->getRequest();

        $event->getManager()->trigger('db.metadata.update', array(
            'metadata' => json_decode($request->getContent(), true),
        ));

        $model = new Model\ArrayModel();
        $model->setData(array(
            'imageIdentifier' => $request->getImageIdentifier(),
        ));

        $event->getResponse()->setModel($model);
    }

    /**
     * Handle GET requests
     *
     * @param EventInterface $event The current event
     */
    public function get(EventInterface $event) {
        $event->getManager()->trigger('db.metadata.load');
    }

    /**
     * Validate metadata found in the request body
     *
     * @param EventInterface $event The event instance
     * @throws InvalidArgumentException
     */
    public function validateMetadata(EventInterface $event) {
        $request = $event->getRequest();
        $metadata = $request->getContent();

        if (empty($metadata)) {
            throw new InvalidArgumentException('Missing JSON data', 400);
        } else {
            $metadata = json_decode($metadata, true);<commit_msg>Fixed implementation<commit_after><?php
/**
 * This file is part of the Imbo package
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE file that was
 * distributed with this source code.
 */

namespace Imbo\Resource;

use Imbo\EventManager\EventInterface,
    Imbo\Exception\InvalidArgumentException,
    Imbo\Model;

/**
 * Metadata resource
 *
 * @author <NAME> <<EMAIL>>
 * @package Resources
 */
class Metadata implements ResourceInterface {
    /**
     * {@inheritdoc}
     */
    public function getAllowedMethods() {
        return array('GET', 'POST', 'PUT', 'DELETE', 'HEAD');
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        return array(
            'metadata.head' => 'get',
            'metadata.get' => 'get',
            'metadata.post' => array(
                'post',
                'validateMetadata' => 10,
            ),
            'metadata.put' => array(
                'put',
                'validateMetadata' => 10,
            ),
            'metadata.delete' => 'delete',
        );
    }

    /**
     * Handle DELETE requests
     *
     * @param EventInterface $event The current event
     */
    public function delete(EventInterface $event) {
        $event->getManager()->trigger('db.metadata.delete');
        $event->getResponse()->setModel(new Model\ArrayModel());
    }

    /**
     * Handle PUT requests
     *
     * @param EventInterface $event The current event
     */
    public function put(EventInterface $event) {
        $request = $event->getRequest();
        $metadata = json_decode($request->getContent(), true);

        $event->getManager()
            ->trigger('db.metadata.delete')
            ->trigger('db.metadata.update', array(
                'metadata' => $metadata,
            ));

        $model = new Model\ArrayModel();
        $model->setData($metadata);

        $event->getResponse()->setModel($model);
    }

    /**
     * Handle POST requests
     *
     * @param EventInterface $event The current event
     */
    public function post(EventInterface $event) {
        $request = $event->getRequest();

        $event->getManager()->trigger('db.metadata.update', array(
            'metadata' => json_decode($request->getContent(), true),
        ));

        $model = new Model\ArrayModel();
        $model->setData($event->getDatabase()->getMetadata($request->getPublicKey(), $request->getImageIdentifier()));

        $event->getResponse()->setModel($model);
    }

    /**
     * Handle GET requests
     *
     * @param EventInterface $event The current event
     */
    public function get(EventInterface $event) {
        $event->getManager()->trigger('db.metadata.load');
    }

    /**
     * Validate metadata found in the request body
     *
     * @param EventInterface $event The event instance
     * @throws InvalidArgumentException
     */
    public function validateMetadata(EventInterface $event) {
        $request = $event->getRequest();
        $metadata = $request->getContent();

        if (empty($metadata)) {
            throw new InvalidArgumentException('Missing JSON data', 400);
        } else {
            $metadata = json_decode($metadata, true);
{'fraction_non_alphanumeric': 0.14224009532320525, 'fraction_numerical': 0.002085195114685731, 'mean_word_length': 2.1960970966206568, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 91, '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': '4132539', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1761, 'n_words': 408}