public function write(&$result)
    {
        $entity = $this->accessor->getPropertyValue($result, $this->definition->container());
        if (empty($entity)) {
            $conditions = [];
            foreach ($this->definition->keys() as $local => $foreign) {
                $conditions[$foreign][] = $this->accessor->getPropertyValue($result, $local);
            }

            $this->cleanup($this->definition->entity(), [], $conditions);
            return $result;
        }

        $this->assertInstance($entity);

        foreach ($this->definition->keys() as $local => $foreign) {
            $this->accessor->setPropertyValue($entity, $foreign, $this->accessor->getPropertyValue($result, $local));
        }

        $this->storage->write($entity, $this->definition->entity())->execute();
        $this->accessor->setPropertyValue($result, $this->definition->container(), $entity);

        return $result;
    }