public function getDiscriminatedClass(PropertyMetadata $mapping, $discriminator = null)
    {
        if ($discriminator === null) {
            $discriminator = $mapping->discriminatorDefaultValue;
        }

        if ($discriminator !== null) {
            if (isset($mapping->discriminatorMap[$discriminator])) {
                $discriminator = $mapping->discriminatorMap[$discriminator];
            }

            return $this->dm->getClassMetadata($discriminator);
        }

        if ($mapping->targetDocument === null) {
            throw ODMException::mixedAssociationRequiresDiscriminator();
        }

        //get the class
        $class = $this->dm->getClassMetadata($mapping->targetDocument);

        //check for single-table inheritance discriminator
        if ($class->discriminatorDefaultValue !== null) {
            $discriminator = $class->discriminatorDefaultValue;
        }

        if ($discriminator !== null) {
            if (isset($class->discriminatorMap[$discriminator])) {
                $discriminator = $class->discriminatorMap[$discriminator];
            }

            $class = $this->dm->getClassMetadata($discriminator);
        }

        return $class;
    }