public function fetch(
        EntityFactoryConfig $config,
        ServerRequestInterface $request,
        ResponseInterface $response,
        callable $next
    ): ResponseInterface {
        $entityRequest = $config->createEntityRequest($request, $this->container);

        # First, we try to determine PK in request path (most common case)
        if (isset($request->getAttribute('routeInfo')[2][$config->getRequestParameterName()])) {
            $entityRequest->setPrimaryKey($request->getAttribute('routeInfo')[2][$config->getRequestParameterName()]);
        }

        # Next, we create the query (ModelCriteria), based on Action class (which can alter the query)
        $query = $this->getQueryFromActiveRecordRequest($entityRequest);

        # Execute beforeFetch hook, which can enforce primary key
        $query = $entityRequest->beforeFetch($query, $request);

        # Now get the primary key in its final form
        $pk = $entityRequest->getPrimaryKey();
        if (null === $pk) {
            $handler = $entityRequest->getContainer()->getEntityRequestErrorHandler();

            return $handler->primaryKeyNotFound($entityRequest, $request,
                $response);
        }

        # Then, fetch object
        $obj = $query->findPk($pk);

        if ($obj === null) {
            $handler = $entityRequest->getContainer()->getEntityRequestErrorHandler();

            return $handler->entityNotFound($entityRequest, $request, $response);
        }

        # Get request params
        if ($config->isHydrateEntityFromRequest()) {
            $params     = $request->getQueryParams();
            $postParams = $request->getParsedBody();
            if ($postParams) {
                $params = array_merge($params, (array)$postParams);
            }

            # Then, alter object with allowed properties
            $obj->fromArray($entityRequest->getAllowedDataFromRequest($params, $request->getMethod()));
        }

        # Then, execute afterFetch hook, which can alter the object
        $entityRequest->afterFetch($obj, $request);

        $request = $request->withAttribute($config->getParameterToInjectInto(), $obj);

        return $next($request, $response);
    }