<commit_before><?php

namespace Presenter;

use Model\Entity\Address;
use Model\Entity\Client;
use Nette\Http\IResponse;

/**
 * Clients resource controller.
 *
 * @author <NAME> <<EMAIL>>
 */
class ClientsPresenter extends SecuredPresenter
{

	/**
	 * Creates a new client entity.
	 */
	public function actionCreate()
	{
		$client = new Client(
			$this->getPost('fullName'),
			$this->getPost('dateOfBirth'),
			$this->getPost('email'),
			$this->getPost('telephone'),
			new Address(
				$this->getPost(['address', 'street']),
				$this->getPost(['address', 'city']),
				$this->getPost(['address', 'zip']),
				$this->getPost(['address', 'country'])
			)
		);

		$this->em->persist($client)->flush();

		$this->sendJson(self::mapClient($client), IResponse::S201_CREATED);
	}

	/**
	 * Updates single client entity identified by id.
	 * @param int $id
	 */
	public function actionUpdate($id)
	{
		$client = $this->em->getRepository(Client::class)->find($id);

		if (!$client) {
			$this->sendError(IResponse::S400_BAD_REQUEST, 'unknownClient');
		}

		$client->fullName    = $this->getPost('fullName');
		$client->dateOfBirth = $this->getPost('dateOfBirth');
		$client->telephone   = $this->getPost('telephone');
		$client->address     = new Address(
			$this->getPost(['address', 'street']),
			$this->getPost(['address', 'city']),
			$this->getPost(['address', 'zip']),
			$this->getPost(['address', 'country'])
		);

		$this->em->flush();

		$this->sendJson(self::mapClient($client));
	}

	/**<commit_msg>refactoring: clients resource does not enforce optional fields<commit_after><?php

namespace Presenter;

use Model\Entity\Address;
use Model\Entity\Client;
use Nette\Http\IResponse;

/**
 * Clients resource controller.
 *
 * @author <NAME> <<EMAIL>>
 */
class ClientsPresenter extends SecuredPresenter
{

	/**
	 * Creates a new client entity.
	 */
	public function actionCreate()
	{
		$email     = $this->getPost('email', NULL);
		$telephone = $this->getPost('telephone', NULL);

		if ($email === NULL && $telephone === NULL) {
			$this->sendError(IResponse::S400_BAD_REQUEST, 'emailOrTelephoneRequired');
		}

		$client = new Client(
			$this->getPost('fullName'),
			$this->getPost('dateOfBirth', NULL),
			$email,
			$telephone,
			new Address(
				$this->getPost(['address', 'street'], NULL),
				$this->getPost(['address', 'city'], NULL),
				$this->getPost(['address', 'zip'], NULL),
				$this->getPost(['address', 'country'], NULL)
			)
		);

		$this->em->persist($client)->flush();

		$this->sendJson(self::mapClient($client), IResponse::S201_CREATED);
	}

	/**
	 * Updates single client entity identified by id.
	 * @param int $id
	 */
	public function actionUpdate($id)
	{
		$client = $this->em->getRepository(Client::class)->find($id);

		if (!$client) {
			$this->sendError(IResponse::S400_BAD_REQUEST, 'unknownClient');
		}

		$email     = $this->getPost('email', NULL);
		$telephone = $this->getPost('telephone', NULL);

		if ($email === NULL && $telephone === NULL) {
			$this->sendError(IResponse::S400_BAD_REQUEST, 'emailOrTelephoneRequired');
		}

		$client->fullName    = $this->getPost('fullName');
		$client->dateOfBirth = $this->getPost('dateOfBirth', NULL);
		$client->email       = $email;
		$client->telephone   = $telephone;
		$client->address     = new Address(
			$this->getPost(['address', 'street'], NULL),
			$this->getPost(['address', 'city'], NULL),
			$this->getPost(['address', 'zip'], NULL),
			$this->getPost(['address', 'country'], NULL)
		);

		$this->em->flush();

		$this->sendJson(self::mapClient($client));
	}

	/**
{'fraction_non_alphanumeric': 0.20818156346315494, 'fraction_numerical': 0.0050434295320818156, 'mean_word_length': 4.3523238380809595, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 73, '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': '4902309', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1190, 'n_words': 215}