/**
* Create a new item.
*
* @param Request $request
* @param string|int $id
*
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws UserProtectedException
* @throws UserLimitReachedException
*
* @return mixed
*/
public function __invoke(Request $request, $id)
{
/**
* @var $resourceType ResourceInterface
*/
list($resourceType, $format) = $this->extractAttributes($request);
/*
* Workaround to ensure stockLevels are not overwritten in a PUT request.
* @see https://github.com/partkeepr/PartKeepr/issues/551
*/
$data = json_decode($request->getContent(), true);
if (array_key_exists('stockLevels', $data)) {
unset($data['stockLevels']);
}
$requestData = json_encode($data);
$data = $this->getItem($this->dataProvider, $resourceType, $id);
$context = $resourceType->getDenormalizationContext();
$context['object_to_populate'] = $data;
/**
* @var $part Part
*/
$part = $this->serializer->deserialize($requestData, $resourceType->getEntityClass(), $format, $context);
if (!$this->partService->isInternalPartNumberUnique($part->getInternalPartNumber(), $part)) {
throw new InternalPartNumberNotUniqueException();
}
return $part;
}