<commit_before><?php namespace App\Http\Controllers;

use App\Auth\UserProvider;
use App\Http\Controllers\Controller;
use App\Models\Plant;
use App\Models\UserPlantData;
use Doctrine\Common\Persistence\ObjectManager as DB;
use Illuminate\Http\Request;
use Respect\Validation\Validator as v;

class PlantsController extends Controller
{
    /**
     * The current user
     * @var App\Models\User
     */
    private $user;

    /**
     * The database
     * @var Doctrine\Common\Persistence\ObjectManager
     */
    private $db;

    public function __construct(UserProvider $userProvider, DB $db)
    {
        $this->user = $userProvider->getCurrentUser();
        $this->db = $db;

        $this->middleware('auth');
        $this->middleware('accept-json');
        $this->middleware('provide-json', ['only' => 'updateUserPlantData']);
    }

    public function getPlants()
    {
        $plants = $this->db->getRepository(Plant::class)
            ->findAllForUser($this->user->getId());

        $plantsJson = array_map(
            [self::class, 'plantToJson'],
            $plants);

        return response()->json($plantsJson);
    }

    public function createPlant(Request $request)
    {
        $json = $request->json()->all();

        $this->validatePlantJson($json);

        $plant = new Plant($json['name'], $this->user);
        $this->db->persist($plant);

        $this->updatePlantFromJson($plant, $json);

        $this->db->flush();

        return response()->json(self::plantToJson($plant));
    }

    public function getPlant(Request $request)
    {
        $plant = $this->db->getRepository(Plant::class)
            ->findForUser($name, $this->user->getId());

        return response()->json(self::plantToJson($plant));
    }

    public function updatePlant(Request $request, $name)
    {
        $json = $request->json()->all();

        $this->validatePlantJson($json);

        $plant = $this->db->getRepository(Plant::class)
            ->findForUser($name, $this->user->getId());
        if (!$plant) {
            abort(404);
        }

        $this->updatePlantFromJson($plant, $json);

        $this->db->flush();

        return response()->json(self::plantToJson($plant));
    }

    private function validatePlantJson($json)
    {
        $validator = v::arrType()->
            keySet(
                v::key('name', v::strType()->length(2, 100)),
                v::key('growTime', v::strType()->length(1, 5)),
                v::key('harvestTime', v::strType()->length(1, 5)),
                v::key('difficulty', v::intVal()->between(1, 5)),
                v::key('taste', v::intVal()->between(1, 5)),
                v::key('rarity', v::intVal()->between(1, 3)),
                v::key('pricePerUnit', v::numericVal()->min(0.01, true)),
                v::key('unit', v::strType()->length(null, 20), false),
                v::key('unitPerSquareFoot', v::numericVal()->min(0.001, true)),
                v::key('notes', v::strType()->length(null, 65536), false),
                v::key('label', v::strType()->length(null, 50), false),
                v::key('links', v::arrType()->key('self', v::url()), false),
                v::key('value', v::numericVal(), false)
            );
        
        $validator->check($json);
    }

    private function updatePlantFromJson(Plant $plant, array $json)
    {
        $plant->setGrowTime($json['growTime']);
        $plant->setHarvestTime($json['harvestTime']);
        $plant->setDifficulty($json['difficulty']);
        $plant->setTaste($json['taste']);
        $plant->setRarity($json['rarity']);
        $plant->setPricePerUnit($json['pricePerUnit']);
        $plant->setUnit($json['unit']);
        $plant->setUnitPerSquareFoot($json['unitPerSquareFoot']);
        $plant->setNotes($json['notes']);
        $plant->setLabel($json['label']);
    }

    private static function plantToJson(Plant $plant)
    {
        return [
            'name'              => $plant->getName(),
            'growTime'          => $plant->getGrowTime(),
            'harvestTime'       => $plant->getHarvestTime(),
            'difficulty'        => $plant->getDifficulty(),
            'taste'             => $plant->getTaste(),
            'rarity'            => $plant->getRarity(),
            'pricePerUnit'      => $plant->getPricePerUnit(),
            'unit'              => $plant->getUnit(),
            'unitPerSquareFoot' => $plant->getUnitPerSquareFoot(),
            'notes'             => $plant->getNotes(),
            'label'             => $plant->getLabel(),
            'value'             => $plant->value(),

            'links'             => [
                'self'          => route('getPlant', ['name' => $plant->getName()]),
            ],
        ];
    }
}
<commit_msg>Getting an unknown plant should return a 404<commit_after><?php namespace App\Http\Controllers;

use App\Auth\UserProvider;
use App\Http\Controllers\Controller;
use App\Models\Plant;
use App\Models\UserPlantData;
use Doctrine\Common\Persistence\ObjectManager as DB;
use Illuminate\Http\Request;
use Respect\Validation\Validator as v;

class PlantsController extends Controller
{
    /**
     * The current user
     * @var App\Models\User
     */
    private $user;

    /**
     * The database
     * @var Doctrine\Common\Persistence\ObjectManager
     */
    private $db;

    public function __construct(UserProvider $userProvider, DB $db)
    {
        $this->user = $userProvider->getCurrentUser();
        $this->db = $db;

        $this->middleware('auth');
        $this->middleware('accept-json');
        $this->middleware('provide-json', ['only' => 'updateUserPlantData']);
    }

    public function getPlants()
    {
        $plants = $this->db->getRepository(Plant::class)
            ->findAllForUser($this->user->getId());

        $plantsJson = array_map(
            [self::class, 'plantToJson'],
            $plants);

        return response()->json($plantsJson);
    }

    public function createPlant(Request $request)
    {
        $json = $request->json()->all();

        $this->validatePlantJson($json);

        $plant = new Plant($json['name'], $this->user);
        $this->db->persist($plant);

        $this->updatePlantFromJson($plant, $json);

        $this->db->flush();

        return response()->json(self::plantToJson($plant));
    }

    public function getPlant(Request $request)
    {
        $plant = $this->db->getRepository(Plant::class)
            ->findForUser($name, $this->user->getId());
        if (!$plant) {
            abort(404);
        }

        return response()->json(self::plantToJson($plant));
    }

    public function updatePlant(Request $request, $name)
    {
        $json = $request->json()->all();

        $this->validatePlantJson($json);

        $plant = $this->db->getRepository(Plant::class)
            ->findForUser($name, $this->user->getId());
        if (!$plant) {
            abort(404);
        }

        $this->updatePlantFromJson($plant, $json);

        $this->db->flush();

        return response()->json(self::plantToJson($plant));
    }

    private function validatePlantJson($json)
    {
        $validator = v::arrType()->
            keySet(
                v::key('name', v::strType()->length(2, 100)),
                v::key('growTime', v::strType()->length(1, 5)),
                v::key('harvestTime', v::strType()->length(1, 5)),
                v::key('difficulty', v::intVal()->between(1, 5)),
                v::key('taste', v::intVal()->between(1, 5)),
                v::key('rarity', v::intVal()->between(1, 3)),
                v::key('pricePerUnit', v::numericVal()->min(0.01, true)),
                v::key('unit', v::strType()->length(null, 20), false),
                v::key('unitPerSquareFoot', v::numericVal()->min(0.001, true)),
                v::key('notes', v::strType()->length(null, 65536), false),
                v::key('label', v::strType()->length(null, 50), false),
                v::key('links', v::arrType()->key('self', v::url()), false),
                v::key('value', v::numericVal(), false)
            );
        
        $validator->check($json);
    }

    private function updatePlantFromJson(Plant $plant, array $json)
    {
        $plant->setGrowTime($json['growTime']);
        $plant->setHarvestTime($json['harvestTime']);
        $plant->setDifficulty($json['difficulty']);
        $plant->setTaste($json['taste']);
        $plant->setRarity($json['rarity']);
        $plant->setPricePerUnit($json['pricePerUnit']);
        $plant->setUnit($json['unit']);
        $plant->setUnitPerSquareFoot($json['unitPerSquareFoot']);
        $plant->setNotes($json['notes']);
        $plant->setLabel($json['label']);
    }

    private static function plantToJson(Plant $plant)
    {
        return [
            'name'              => $plant->getName(),
            'growTime'          => $plant->getGrowTime(),
            'harvestTime'       => $plant->getHarvestTime(),
            'difficulty'        => $plant->getDifficulty(),
            'taste'             => $plant->getTaste(),
            'rarity'            => $plant->getRarity(),
            'pricePerUnit'      => $plant->getPricePerUnit(),
            'unit'              => $plant->getUnit(),
            'unitPerSquareFoot' => $plant->getUnitPerSquareFoot(),
            'notes'             => $plant->getNotes(),
            'label'             => $plant->getLabel(),
            'value'             => $plant->value(),

            'links'             => [
                'self'          => route('getPlant', ['name' => $plant->getName()]),
            ],
        ];
    }
}

{'fraction_non_alphanumeric': 0.18632319470308298, 'fraction_numerical': 0.0074487895716946, 'mean_word_length': 2.197816738339398, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 189, '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': '4727089', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2809, 'n_words': 438}